https://kotlinlang.org logo
#android
Title
# android
r

ReddyTintaya

05/25/2020, 11:38 PM
Hi, i start a picker with this code. i want to get the path for then read that file and send it to my backend with multipart using this line, in a xiaomi device works fine, there the path looks like this "/storage/emulated/0/Download/images.jpeg"
Copy code
File(photoPath).asRequestBody("image/jpg".toMediaTypeOrNull())
//REQUEST_CODE_PICK_IMAGE = 101
Copy code
Intent(Intent.ACTION_PICK).also {
    it.type = "*/*"
    val mimeTypes = arrayOf("image/jpeg", "image/png", "application/pdf")
    it.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
    startActivityForResult(it, REQUEST_CODE_PICK_IMAGE)
}
and then i use this
Copy code
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (resultCode == Activity.RESULT_OK) {
        when (requestCode) {
            REQUEST_CODE_PICK_IMAGE -> {
                selectedImageUri = data?.data
                Log.i("develop", "data: ... $data")
                image.setImageURI(selectedImageUri)
                homeViewModel.selectedImageUri = selectedImageUri?.lastPathSegment.toString()
            }
        }
    }
}
My problem is when i use the emulator an it uses google photos for picking the image and the path generated looks like this "content://com.android.providers.downloads.documents/document/msf%3A31" and when im trying to read i get the error that the file does not exists. is there aa way for parsin from "content://...." to "storage/....." path?
a

Adam Powell

05/26/2020, 12:31 AM
No, they cannot be converted. Content uris are relative to the package they refer to in the host segment and that package always brokers access to the content. The system gives you a temporary permission grant to access it, and you should open it with the ContentResolver when you get the activity result.
r

ReddyTintaya

05/26/2020, 12:32 AM
@Adam Powell so is there a way for opening that URI for getting this?
Copy code
File(photoPath).asRequestBody("image/jpg".toMediaTypeOrNull())
It does not have to be a file. The app hosting the
ContentProvider
can provide it as a virtual file if it so chooses, so
File()
cannot work
11 Views