Querying the MediaStore: Joining thumbnails and images (on ID)

前端 未结 3 782
情深已故
情深已故 2021-02-15 12:38

I\'m working on a \"photo gallery\"-type app for Android. It started as a Final Project for the Developing Android Apps at Udacity, so it\'s overall structure (activities, conte

3条回答
  •  北荒
    北荒 (楼主)
    2021-02-15 13:15

    The accepted answer got me started on this question, but it contain a couple of small errors.

    case LEFT:
                // handle case where a row in cursorA is unique
                // images is unique (missing thumbnail)
    case RIGHT:
                // handle case where a row in cursorB is unique
                // thumbs is unique (missing image)
    

    These are backwards. The documentation contradicts itself, and is likely where the mistake got made. From the source code of CursorJoiner:

    case LEFT:
            // handle case where a row in cursorA is unique
    

    Then in the enum for Result from the source code:

    public enum Result {
        /** The row currently pointed to by the left cursor is unique */
        RIGHT,
        /** The row currently pointed to by the right cursor is unique */
        LEFT,
        /** The rows pointed to by both cursors are the same */
        BOTH
    }
    

    So I am guessing this is why you were force incrementing the cursors.

     //compensate for CursorJoiner expecting cursors ordered ascending...
                    c_images.moveToNext();
                    c_thumbs.moveToPrevious();
    

    The iterator in CursorJoiner automatically increments the cursors for you.

    This should be the working code (This code will also merge internal storage and external storage into a single cursor):

            Cursor[] thumbs = new Cursor[2];
            thumbs[0] = mActivity.getContentResolver().query(
                    MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                    new String[]{
                            MediaStore.Images.Thumbnails._ID ,
                            MediaStore.Images.Thumbnails.IMAGE_ID,
                            MediaStore.Images.Thumbnails.DATA
                    },
                    null,
                    null,
                    MediaStore.Images.Thumbnails.IMAGE_ID + "*(-1)"
            );
            thumbs[1] = mActivity.getContentResolver().query(
                    MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URI,
                    new String[]{
                            MediaStore.Images.Thumbnails._ID ,
                            MediaStore.Images.Thumbnails.IMAGE_ID,
                            MediaStore.Images.Thumbnails.DATA
                    },
                    null,
                    null,
                    MediaStore.Images.Thumbnails.IMAGE_ID + "*(-1)"
            );
            Cursor thumbCursor = new MergeCursor(thumbs);
            Cursor[] cursors = new Cursor[2];
            cursors[0] = mActivity.getContentResolver().query(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    new String[]{
                            MediaStore.Images.Media._ID,
                            MediaStore.Images.Media.DATA,
                            MediaStore.Images.Media.ORIENTATION,
                            MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
                            MediaStore.Images.Media.BUCKET_ID,
                            MediaStore.Images.Media.MIME_TYPE
                    },
                    null,
                    null,
                    MediaStore.Images.Media._ID + "*(-1)"
            );
            cursors[1] = mActivity.getContentResolver().query(
                    MediaStore.Images.Media.INTERNAL_CONTENT_URI,
                    new String[]{
                            MediaStore.Images.Media._ID,
                            MediaStore.Images.Media.DATA,
                            MediaStore.Images.Media.ORIENTATION,
                            MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
                            MediaStore.Images.Media.BUCKET_ID,
                            MediaStore.Images.Media.MIME_TYPE
                    },
                    null,
                    null,
                    MediaStore.Images.Media._ID + "*(-1)"
            );
            Cursor photoCursor = new MergeCursor(cursors);
            CursorJoiner cursorJoiner = new CursorJoiner(
                    thumbCursor,
                    new String[]{
                            MediaStore.Images.Thumbnails.IMAGE_ID
                    },
                    photoCursor,
                    new String[]{
                            MediaStore.Images.Media._ID,
                    }
            );
            Cursor finalCursor= new MatrixCursor(new String[]{
                    MediaStore.Images.Media._ID,
                    MediaStore.Images.Media.DATA,
                    MediaStore.Images.Media.ORIENTATION,
                    MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
                    MediaStore.Images.Media.BUCKET_ID,
                    MediaStore.Images.Media.MIME_TYPE,
                    "thumb_data"
            });
            for (CursorJoiner.Result joinerResult : cursorJoiner) {
                switch (joinerResult) {
                    case RIGHT:
                        finalCursor.addRow(new Object[]{
                                photoCursor.getLong(photoCursor.getColumnIndex(MediaStore.Images.Media._ID)),
                                photoCursor.getString(photoCursor.getColumnIndex(MediaStore.Images.Media.DATA)),
                                photoCursor.getLong(photoCursor.getColumnIndex(MediaStore.Images.Media.ORIENTATION)),
                                photoCursor.getString(photoCursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME)),
                                photoCursor.getLong(photoCursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID)),
                                photoCursor.getString(photoCursor.getColumnIndex(MediaStore.Images.Media.MIME_TYPE)),
                                null
                        });
                        break;
                    case BOTH:
                        finalCursor.addRow(new Object[]{
                                photoCursor.getLong(photoCursor.getColumnIndex(MediaStore.Images.Media._ID)),
                                photoCursor.getString(photoCursor.getColumnIndex(MediaStore.Images.Media.DATA)),
                                photoCursor.getLong(photoCursor.getColumnIndex(MediaStore.Images.Media.ORIENTATION)),
                                photoCursor.getString(photoCursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME)),
                                photoCursor.getLong(photoCursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID)),
                                photoCursor.getString(photoCursor.getColumnIndex(MediaStore.Images.Media.MIME_TYPE)),
                                thumbCursor.getString(thumbCursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA)),
                        });
                        break;
                }
    
            }
            photoCursor.close();
            thumbCursor.close();
    

提交回复
热议问题