0

I am trying to the get the real path of my image from its uri but getting null in return as path. I am using the following referenced code to get the real path.

https://gist.github.com/tatocaster/32aad15f6e0c50311626

Sample Image Uri: content://com.android.providers.media.documents/document/image%3A102173

Returned Real Path: null

Moeez Atlas
  • 116
  • 2
  • 11
  • What are you trying to do with the real path of the image? For security reasons you are prevented from getting an absolute path to files in newer android APIs – Rafsanjani Mar 31 '22 at 12:00
  • Very well. You should not try to get 'a real path'. You have a nice content scheme uri. You can use it for all file actions. – blackapps Mar 31 '22 at 12:00
  • @Rafsanjani, Has nothing to do with security. If you do it right you will get a nice file system path. The path will be correct but not usable as the app will not have a read permission for it. Hence trying to get 'a real path' is useless. Thats all. – blackapps Mar 31 '22 at 12:07

1 Answers1

0

Try this it didn't work for me but you can try Get filename and path from URI from mediastore as it has apparently worked for many people's. If you still get null you can do what I have done.

I tried many ways but I was still getting the result null. basically, I am trying to upload an image from local storage compress it, and used the compressed image.

val path = this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS)`try {
                        val file = File(path, "/Image")
                        file.mkdirs()
                        val image = File(file.absolutePath, "image1.jpeg")
                        intent1.data = image.absolutePath.toUri()
                        val bitmap = BitmapFactory.decodeStream(this.contentResolver.openInputStream(uri))
                        val array = ByteArrayOutputStream()
                        val out =  FileOutputStream(image)
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 70,out)
                        out.flush()
                        out.close()
                        Log.d("Path", image.absolutePath)
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }`
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 10 '22 at 05:36