EDIT 2
I think i found the solution of the problem. It is mentioned in the Google docs that accessing a shared file will give you the URI.
The server app sends the file's content URI back to the client app in an Intent. This Intent is passed to the client app in its override of onActivityResult(). Once the client app has the file's content URI, it can access the file by getting its FileDescriptor.
Below is the updated code i am using inside onActivityResult. Make sure to call the super method of onActivityResult at last.
super.onActivityResult(requestCode, resultCode, data)
Working code
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
data?.data?.let {
util._log(TAG, it.toString())
}
if (data!!.data != null && data.data != null) {
try {
val stream = if (data.data!!.toString().contains("com.google.android.apps.photos.contentprovider")) {
val ff = contentResolver.openFileDescriptor(data.data!!, "r")
FileInputStream(ff?.fileDescriptor)
} else {
contentResolver.openInputStream(data.data!!)
}
val createFile = createImageFile()
util.copyInputStreamToFile(stream, createFile)
selectedImagePath = createFile.absolutePath
} catch (e: Exception) {
util._log(TAG, Log.getStackTraceString(e))
}
}
super.onActivityResult(requestCode, resultCode, data)
}
EDIT
Also check this stackoverflow post
Original
I am using it on Android oreo 8.1.0(API 27) on my Redmi 6 pro phone and it is working fine.
You haven't posted the onActivityResult method may be this is where you need to do some modifications. I have tried it both
Below is my code snippet
val pickIntent = Intent(Intent.ACTION_VIEW)
pickIntent.type = "image/*"
pickIntent.action = Intent.ACTION_GET_CONTENT
pickIntent.addCategory(Intent.CATEGORY_OPENABLE)
startActivityForResult(pickIntent, SELECT_PICTURE)
and in onActivityResult i am parsing it like this
if (data!!.data != null && data.data != null) {
try {
// CommonUtilities._Log(TAG, "Data Type " + data.getType());
if (!isFinishing) {
val inputStream = contentResolver.openInputStream(data.data!!)
val createFile = createImageFile()
copyInputStreamToFile(inputStream!!, createFile)
// CommonUtilities._Log(TAG, "File Path " + createFile.getAbsolutePath());
selectedImagePath = createFile.absolutePath
}
} catch (e: IOException) {
util._log(TAG, Log.getStackTraceString(e))
}
}
Method to create a new file
@Throws(IOException::class)
private fun createImageFile(): File {
// Create an image file name
val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(Date())
val imageFileName = "yesqueen_" + timeStamp + "_"
val storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
return File.createTempFile(imageFileName, ".jpg", storageDir)
}
Method to read from inputstream
fun copyInputStreamToFile(`in`: InputStream, file: File) {
var out: OutputStream? = null
try {
out = FileOutputStream(file)
val buf = ByteArray(1024)
var len: Int = 0
while (`in`.read(buf).apply { len = this } > 0) {
out.write(buf, 0, len)
}
/*while (`in`.read(buf).let {
len = it
true
}) {
out.write(buf, 0, len)
}*/
/* while ((len = `in`.read(buf)) > 0) {
out.write(buf, 0, len)
}*/
} catch (e: Exception) {
e.printStackTrace()
} finally {
try {
out?.close()
} catch (e: Exception) {
e.printStackTrace()
}
try {
`in`.close()
} catch (e: Exception) {
e.printStackTrace()
}
}
}