4

I'm using Android new photo picker and I'm trying to get the photo's real file location, but the photo picker gives me the following URI content://media/picker/0/com.android.providers.media.photopicker/media/1000000020

I've also tried the below to get the real file location with the following, but that also seems to give me an inaccurate location when I check the image in files.

@Composable
private fun getRealPathFromURI(contentUri: Uri): String {
    val projection = arrayOf(MediaStore.Images.Media.DATA)
    val context = LocalContext.current
    val cursor = context.contentResolver.query(contentUri, projection, null, null, null)
    val columnIndex = cursor?.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
    cursor?.moveToFirst()
    val filePath = cursor?.getString(columnIndex!!)
    cursor?.close()
    println("filepath $filePath")
    return filePath!!
}

/sdcard/.transforms/synthetic/picker/0/com.android.providers.media.photopicker/media/1000000003.jpg

Is this not possible?

Robert
  • 809
  • 3
  • 10
  • 19
  • 2
    "I'm trying to get the photo's real file location" -- there is no requirement for the photo to exist on the filesystem, let alone at a path that you have read access to. Please use the `Uri` as intended, such as via `ContentResolver` and `openInputStream()` to read in the contents. – CommonsWare Feb 05 '23 at 00:38
  • For newer developers you are not very helpful on how to accomplish this. – Robert Feb 05 '23 at 00:53
  • That is because "this" ("get the photo's real file location") is not possible in any sort of reliable form. If you want to display the image, use an image-loading library like Glide or Picasso, which can work with a `Uri` like this. If you want to upload the image, I linked you to [a question](https://stackoverflow.com/questions/56308559/create-a-file-from-a-photo-uri-on-android) that deals with that, and the answer links to [a blog post](https://commonsware.com/blog/2020/07/05/multipart-upload-okttp-uri.html) with a sample project. – CommonsWare Feb 05 '23 at 01:02
  • I've been struggling with this problem as you. – AdamPI Jul 16 '23 at 12:26
  • Facing same problem – Ahsan Ullah Rasel Aug 22 '23 at 06:58

0 Answers0