1Answer

I have been retrieving images from MediaStore in the following way...

Uri uriExternal = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

String[] projection = {
     MediaStore.MediaColumns._ID,
     MediaStore.MediaColumns.DATE_ADDED
};

Cursor cursor = getContentResolver()
    .query(uriExternal, projection,
           MediaStore.MediaColumns.DATA + " IS NOT NULL",
           null,
           MediaStore.MediaColumns.DATE_ADDED + " DESC");

if(cursor != null) {
    while (cursor.moveToNext()) {
        String _id = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns._ID));

         paths.add(uriExternal.toString() + "/" + _id);
    }

    cursor.close();
}

Basically, I'm simply appending the file id to the external content provider uri. This makes a uri that I can use with content providers...

content://media/external/images/media/{id}

It all works perfectly fine, all external images are displayed and loaded flawlessly. However, since I've failed to find proper documentation, I'm a little concerned I'm not doing things the proper way. Especially because of the way I'm constructing the uri...kind of hard-coding it...

The questions are...

Is this the correct way to construct a content uri for an external image?

Is there a more reliable way to achieve this?

Answer

Personally, I use ContentUris.withAppendedId(). That way, I don't have to worry about whether I am starting with a Uri that ends in / or not. :-)

In general, MediaStore adheres to the original ContentProvider vision of using the content ID as the last path segment of a Uri pointing to the content. However, that is not a general rule, and it will not work for all providers.

  • 1
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions