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.
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.
If not, what should I expect in the DATA
column, and how to get an Uri from it?
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)));