Getting the real path from Uri

Earlier this year, I was working on a freelance project that required me to do the following

  1. Allow the user to open up one of the Image viewing apps when they click a button
  2. Allow the user to select one image
  3. Once the user selects an image, take that image, and display the image on an Activity layout
  4. If the user is happy with the image, allow the user to upload that photo.

Sounds simple right? You just fire up an intent, and then you override the onActivityResult method upon user’s selection of the image, and then you load up the image onto your layout using your favorite Image loading library (I use Picasso). The code for the onActivityResult looks something like this.

This part is pretty straightforward. The insanity starts when you want to take the resulting image file and then upload it to your cloud storage like AWS S3. A sane developer might think that a method like this would exist on the profilePhotoUri

But it doesn’t. Instead, you have to instantiate a cursor and perform some type of surgery on your resulting photoUri that you get back on your onActivityResult.

This was my face after learning about this 😑😑😑😑😑😑😑😑

About the Author Chris Jeon

Software developer currently focusing on Android development.

  • Cesar Lopez

    Hi, thanks for the hint.

    Using the emulator API 25, cursor contains this columns,
    mColumns = {String[6]@5704}
    0 = “document_id”
    1 = “mime_type”
    2 = “_display_name”
    3 = “last_modified”
    4 = “flags”
    5 = “_size”

    so
    int indx = – 1 because no MediaStore.Images.ImageColumns.DATA exists.

    How did you solve it?

    • Hmm, I don’t really understand what you mean by indx = -1. Can you elaborate on that?