Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

android - Access ordered images and video in same Cursor

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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...