Android media thumbnails. Serious issues?
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 开发者_Python百科= 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?
you can solve your problem querying MediaStore.Images.Media._ID instead of thumbnails' content provider
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.
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);
}
}
精彩评论