2

I've been playing with android's thumbnails for a while now, and I've seen some inconsistencies that make me want to scream. My goal is to have a simple list of all Images (and a separate list for video) with the thumbnail and filename.

Device: HTC Evo (fresh from Google I/o)

First off: http://androidsamples.blogspot.com/2009/06/how-to-display-thumbnails-of-images.html

That code doesn't seem to work at all, thumbnails are duplicated... some with the "mirror" effect and some without. Also some won't load and just display a black square. I've tried rebuilding the thumbnails by deleting the "alblum thumbs" directory from the SD card. HTC's gallery application seem to show everything fine.

This approach seems to work:

Bitmap thumb = MediaStore.Images.Thumbnails.getThumbnail(
    getContentResolver(),
    id, MediaStore.Video.Thumbnails.MICRO_KIND, null);
imageView.setImageBitmap(curThumb);

where id is the original images id and imageView is some image view. This is great! But, strangely, way too slow to be used inside a SimpleViewBinder. Next approach:

String [] proj = {MediaStore.Images.Thumbnails._ID};
Cursor c = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
    proj, MediaStore.Images.Thumbnails.IMAGE_ID + "=" +id ,
    null, null);

if (c != null && c.moveToFirst()) {
    Uri thumb = Uri.withAppendedPath(mThumbUri,c.getLong(0)+"");
    imageView.setImageURI(thumb);
}

I should explain that I feel the needed WHERE condition is required because there doesn't seem to be any guarantee that your uri will have the same ID for both a thumbnail and its parent image. This works for all of the current images, but as soon as I start adding pictures with the camera they show up as blank! Debugging shows a dreaded:

SkImageDecoder::Factory returned null

error and the URI is returned as invalid. These are the same images that work with the previous call. Can anyone either catch my logical failure or point me to some working code?

Ralphleon
  • 3,968
  • 5
  • 32
  • 34
  • I went with the first approach and used a thread to actually load the thumbnail. The Uri approach is so much faster though... it's a shame I can't figure it out! – Ralphleon May 26 '10 at 00:08

3 Answers3

1

you can solve your problem querying MediaStore.Images.Media._ID instead of thumbnails' content provider

dodopepper
  • 11
  • 1
1

From my experience the thumbnail content provider will return a content URI and path to image file, even if that thumbnail has not yet been created. The MediaStore.Images.Thumbnails.getThumbnail() call will actually create the thumbnail (and put it where the content URI is pointing already) if one doesn't exist. This wasn't clear to me in the documentation but seems to be what's happening.

Matt Hall
  • 2,569
  • 2
  • 23
  • 33
  • The uri is returned once the image file is registered to the database and it will just extract the thumbnail in the background. If you call MediaStore.images.thumbnails.getThumbnail() and the thumbnail is not yet finished, it will try to extract the thumbnail directly from the file and not from the database. – artsylar Dec 14 '11 at 05:55
1

Load video thumbnail where mThumbButton is an ImageButton:

public void loadThumb(int videoId)
    {
        String [] proj = {MediaStore.Video.Thumbnails._ID};
        Cursor c = managedQuery(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
            proj, MediaStore.Video.Thumbnails.VIDEO_ID + "=" +videoId ,
            null, null);

        if (c != null && c.moveToFirst()) {
            Uri thumb = Uri.withAppendedPath(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,c.getLong(0)+"");
            mThumbButton.setImageURI(thumb);
        }

    }
Burt
  • 11
  • 1