How to query Android MediaStore Content Provider, avoiding orphaned images?

前端 未结 2 1362
栀梦
栀梦 2020-12-02 06:34

I\'m trying to provide an in-app Activity which displays thumbnails of photos in the device\'s media store, and allow the user to select one. After the user makes a selecti

相关标签:
2条回答
  • 2020-12-02 07:07

    Okay, I've found the problem with this code sample.

    In the onCreate() method, I had this line:

    mImageCursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                                 projection, selection, selectionArgs, null );
    

    The problem here is that it's querying for the thumbnails, rather than the actual images. The camera app on HTC devices does not create thumbnails by default, and so this query will fail to return images that do not already have thumbnails calculated.

    Instead, query for the actual images themselves:

    mImageCursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                 projection, selection, selectionArgs, null );
    

    This will return a cursor containing all the full-sized images on the system. You can then call:

    Bitmap bm = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(),
            imageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
    

    which will return the medium-sized thumbnail for the associated full-size image, generating it if necessary. To get the micro-sized thumbnail, just use MediaStore.Images.Thumbnails.MICRO_KIND instead.

    This also solved the problem of finding thumbnails that have dangling references to the original full-sized images.

    0 讨论(0)
  • 2020-12-02 07:20

    Please note that things will be changing soon, managedQuery method is deprecated. Use CursorLoader instead(since api level 11).

    0 讨论(0)
提交回复
热议问题