3

Sorry if it's a stupid question. Am confused. In my Android App, am trying to get the path to the image chosen by the user from the Gallery. Earlier, I was using MediaStore.Images.ImageColumns.DATA to get the chosen image's path like this:

    cursor = getContentResolver().query(contentURI, projection, null,
            null, null);
    if (cursor == null) { // Source is Dropbox or other similar local filepath
        Log.d(logtag, "getRealPathFromURI : cursor null");
        return contentURI.getPath();

    } else {
        cursor.moveToFirst();
        int idx = cursor
                .getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        String path = cursor.getString(idx);
        cursor.close();
        return path;
    }

But this caused issues in certain devices, i.e the cursor returned that there was no such column. So, after referring some Stackoverflow answers, I changed it from MediaStore.Images.ImageColumns.DATA to MediaStore.Images.Media.DATA. Now, this seems to work. What's the difference between MediaStore.Images.ImageColumns.DATA and MediaStore.Images.Media.DATA? And I don't think this has to do with Kitkat version, because both MediaStore.Images.ImageColumns.DATA and MediaStore.Images.Media.DATA existed from API level 1. I tried searching, but couldn't get any useful info. Please help.

Jean
  • 2,611
  • 8
  • 35
  • 60
  • possible duplicate of [Android Gallery on KitKat returns different Uri for Intent.ACTION\_GET\_CONTENT](http://stackoverflow.com/questions/19834842/android-gallery-on-kitkat-returns-different-uri-for-intent-action-get-content) – Juanjo Vega Aug 25 '15 at 09:54
  • why you are not using Environment.getDataDirectory() ? – Sami Aug 25 '15 at 10:03
  • 2
    Why do you think that there is a path that you can use, anyway? [A `Uri` is not a file](https://commonsware.com/blog/2014/07/04/uri-not-necessarily-file.html). Use the `Uri` with a `ContentResolver` and methods like `getType()` and `openInputStream()` to use the content identified by that `Uri`. – CommonsWare Aug 25 '15 at 10:53
  • The problem that i faced was similar ,and issue in my case was that i was testing on Lollipop and in android lollipop it showed me images from Recent Tab, which were cached images and were not preset, so When i went to gallery and selected the same image it worked fine. Hope this helps you to resolve the issue. – PravinDodia Feb 28 '17 at 15:03

1 Answers1

0

There is a hierarchical relationship between MediaStore.Images.Media and MediaStore.Images.ImageColumns.

MediaStore.Images.Media -> MediaStore.Images.ImageColumns -> MediaStore.MediaColoumns.

As you can see the base class for both Media and ImageCoulumns is MediaCoulumns and Data is a field from that class. So it shouldn't matter through which class you use it. It is weird that replacing the one with other is solving the problem.

yeshu
  • 192
  • 1
  • 10