Getting the Right Uri

A MediaStore Uri, pointing to an individual piece of content, is the combination of a collection Uri and the ID of the content. The classic way to accomplish that was via ContentUris.withAppendedId(), which assembles the MediaStore Uri given those two pieces.

We now have an alternative: a two-parameter getContentUri() method on the various MediaStore collection classes (e.g., MediaStore.Video.Media). Whereas ContentUris.withAppendedId() takes a collection Uri and an ID, getContentUri() takes a “volume name” and an ID. MediaStore.VOLUME_EXTERNAL works for classic external storage as the volume name, giving us:

val contentUri = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL, id)

where id is the ID of some piece of content (in this case, a video).


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.