Access ordered images and video in same Cursor

后端 未结 1 336
不思量自难忘°
不思量自难忘° 2020-11-30 22:29

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

相关标签:
1条回答
  • 2020-11-30 23:19

    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();
    
    0 讨论(0)
提交回复
热议问题