Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Content Uri Format

Tags:

android

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?

like image 380
Leo Avatar asked Feb 04 '26 11:02

Leo


1 Answers

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.

like image 194
CommonsWare Avatar answered Feb 05 '26 23:02

CommonsWare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!