-1

I want to get the actual path of an image stored in the gallery of android phone.

In my app, I am selecting an image from the gallery and setting it to an imageView. The reason why I need the path is that later I want to send that image as an attachment through JavaMail.

I am getting quite confused about this.

The image path in my device is:

Internal storage/DCIM/Camera/image_name.jpg

I have used the following method to get the path from the image uri:

String path = imageUri.getPath();

And getting the following result:

raw//storage/emulated/0/DCIM/Camera/image_name.jpg

Now, when I pass the above result as the filepath of the attachment to send, I get the File not found exception

raw//storage/emulated/0/DCIM/Camera/image_name.jpg: open failed: ENOENT (No such file or directory)

With a few trial and errors, the image attachment was successfully sent when I passed the following path as hardcoded string:

/storage/emulated/0/DCIM/Camera/image_name.jpg

I know this type of question has been asked before many times, but I couldn't a satisfactory working answer. I hope, this explanation of my problem might get me the solution.

Ankush Chauhan
  • 1,433
  • 2
  • 17
  • 22
  • "I couldn't a satisfactory working answer" -- that is because there is no "working answer". A `Uri` is not a file and does not have to point to a file, let alone a file that you have access to on the filesystem. This is particularly true on Android 10 and higher, where your access to the filesystem is much more limited. Use the `Uri` itself. For example, if you are trying to display this image, image-loading libraries like Glide and Picasso can use the `Uri` to do so. – CommonsWare Aug 31 '19 at 11:23
  • "No working answer" - the answer by @Rajnish has worked for me. And yes, I am using the Uri to set the image to imageview. But as I clearly mentioned in my question that I needed the path of the image to send it as an email attachment. – Ankush Chauhan Aug 31 '19 at 12:12
  • "the answer by @Rajnish has worked for me" -- apparently, you have not tested on Android 10. Presumably, you have not tested on a wide range of devices for images that are on removable storage. "I clearly mentioned in my question that I needed the path of the image to send it as an email attachment" -- if JavaMail supports `InputStream` for attachments, use it. Otherwise, copy the bytes of the content to a file that you control, then use that file. – CommonsWare Aug 31 '19 at 12:54

1 Answers1

0

Pass the uri that you are getting in onActivityResult and use the returned String.

public String getPathFromURI(Uri ContentUri) {
    String res = null;
    String[] proj = {MediaStore.Images.Media.DATA};
    Cursor cursor = getContentResolver()
            .query(ContentUri, proj, null, null, null);

    if (cursor != null) {
        cursor.moveToFirst();

        res = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
        cursor.close();
    }


    return res;
}
Rajnish suryavanshi
  • 3,168
  • 2
  • 17
  • 23