Get URI from ContentResolver.query()
Asked Answered
R

1

8

I am querying the Images table to get all pictures in the MediaStore.Images.Media.EXTERNAL_CONTENT_URI directory.

See the following query:

String[] what = new String[]{ MediaStore.Images.ImageColumns.DATE_TAKEN,
            MediaStore.Images.ImageColumns._ID,
            MediaStore.Images.ImageColumns.MIME_TYPE,
            MediaStore.Images.ImageColumns.DATA };

String where = MediaStore.Images.Media.MIME_TYPE + "='image/jpeg'" +
            " OR " + MediaStore.Images.Media.MIME_TYPE + "='image/png’";

Cursor cursor = getContext().getContentResolver()
                    .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
                            what,
                            where,
                            null,
                            MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC”);

Now, I’d like to have an Uri pointing to each of the result. This is what I am doing right now:

int dataIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
String path = cursor.getString(dataIndex);
final Uri uri = Uri.fromFile(new File(path));

E.g., I take the path from the DATA column, create a file and use Uri.fromFile. I have two questions.

  1. Is this guaranteed to work? Is the query above guaranteed to return paths in the data column? It works for all the pictures in my phone: path is always a path, like /storage/0/whatever.jpg, and uri.toString() is the same but with the file scheme. Still, pictures can very well be defined by content:// uris, but I fail to see how (and if) these are represented in the images table.

  2. If not, what should I expect in the DATA column, and how to get an Uri from it?

Reynold answered 23/6, 2016 at 19:3 Comment(0)
I
28

Is the query above guaranteed to return paths in the data column?

It should return something. That "something" may not be usable. For example, the image might be on removable storage, and you cannot access it directly.

how to get an Uri from it?

You don't. You construct a Uri from the _ID:

Uri imageUri=
  ContentUris
    .withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
       cursor.getInt(cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID)));
Intersect answered 23/6, 2016 at 19:18 Comment(3)
Interesting, thank you. Just one thing about the “may not be usable part”. As I understand, you mean that I can’t call new File(path) on these. But if I build an Uri as you said, will they be accessible through ContentResolver.open() ?Reynold
Rewording, is the Uri built with ContentUri s safe (i.e. works even in edge cases like images in removable storage) or should I add a check somewhere?Reynold
@mvai: So long as you have READ_EXTERNAL_STORAGE, you should be OK. Though, testing is recommended. :-) I have done more work with videos than still photos, and it seems to be fine there. The worst-case outcome is just what you have with the File approach: no access.Intersect

© 2022 - 2024 — McMap. All rights reserved.