41

I'm using the android.content.CursorLoader class to create two Cursor objects to access media stored on the user of my app's device. I'd like to give the user a grid view of their stored images and video which preserves order from the Android Gallery app.

Currently I'm using one Cursor to access Images and one to access Video. With this approach, all images precede all videos (i.e. they are in two separate groups). Is there a way to access both Images and Video from the same Cursor? If not, is there a better way to access these media on the device?

For reference, here is the code I am using:

For Images:

CursorLoader cursorLoader = new CursorLoader(
    mContext,
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
    IMAGE_PROJECTION,
    null,
    null,
    MediaStore.Images.Media._ID + " desc"
  );
  mImageCursor = cursorLoader.loadInBackground();

And Video:

CursorLoader cursorLoader = new CursorLoader(
    mContext,
    MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
    VIDEO_PROJECTION,
    null,
    null,
    MediaStore.Video.Media._ID + " desc"
  );
  mVideoCursor = cursorLoader.loadInBackground();
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Cam
  • 1,706
  • 2
  • 13
  • 15

1 Answers1

108

After lots of research and playing around with source code, I'm finally a bit more familiar with the Android filesystem. To get a single Cursor which can access information about both Images and Video I used the following:

// Get relevant columns for use later.
String[] projection = {
    MediaStore.Files.FileColumns._ID, 
    MediaStore.Files.FileColumns.DATA,
    MediaStore.Files.FileColumns.DATE_ADDED,
    MediaStore.Files.FileColumns.MEDIA_TYPE,
    MediaStore.Files.FileColumns.MIME_TYPE,
    MediaStore.Files.FileColumns.TITLE
};

// Return only video and image metadata.
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
         + MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE 
         + " OR "
         + MediaStore.Files.FileColumns.MEDIA_TYPE + "="
         + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;

Uri queryUri = MediaStore.Files.getContentUri("external");

CursorLoader cursorLoader = new CursorLoader(
    this,
    queryUri,
    projection,
    selection,
    null, // Selection args (none).
    MediaStore.Files.FileColumns.DATE_ADDED + " DESC" // Sort order.
  );

Cursor cursor = cursorLoader.loadInBackground();
Cam
  • 1,706
  • 2
  • 13
  • 15
  • 4
    thanks for sharing your final code; this was really helpful to me! – scientiffic Dec 20 '13 at 17:15
  • there is an issue, you are accessing the main image / data.. not the thumbnails. Querying MediaStore.Images.Thumbnails for every cursor item is too heavy. Any ideas? – Umair Oct 28 '15 at 10:33
  • i am not getting all videos using this solution... please suggest me whats wrong with this.. e.g : there are 102 video in my gallary but when i fetch using this get only 50 videos – Nitin Nov 19 '16 at 07:16
  • The content uri is for external storage only, probably rest of 52 videos of yours are in internal storage. – Angad Singh Aug 19 '18 at 15:10
  • How to use this cursor in adapter to load all images and videos in recyclerview? – Nabeel Ahmed Feb 25 '20 at 08:12
  • 1
    as for android 10, keep in mind that uris will be "content://media/external/file/..." and android will behave differently than with "content://media/external/images/media/..." when you try to delete or update these uris. for latter android will throw RecoverableSecurityException and you can display system dialog to request access to this file. with "file" uri content resolver will just return 0 when you will try to delete that uri – artman Mar 10 '20 at 12:39