views:

128

answers:

1

I'm using the MediaStore.Video.Thumbnails.getThumbnail() method to fetch thumbnails for files that I am displaying in a list. This works well to begin with, but after a while the thumbnails that I get back are not the ones that match the file. Once GetThumbnail() starts failing it seems to return only the same Bitmap over and over again, regardless of which file I try. This happens on my HTC Desire, running Android 2.2, and is very hard to reproduce - it will just start happening after some time.

Here is (a cut back version of) my source code:

static public Bitmap GetThumbnailForFile(File p_File, Activity
p_activity)
{
    long imageID = GetImageID(p_File, p_activity);
    if (imageID < 0)
    {
        return null;
    }
    return
MediaStore.Images.Thumbnails.getThumbnail(p_activity.getContentResolver(),
imageID, Thumbnails.MICRO_KIND, null);
}

public static long GetImageID(File p_File, Activity p_activity)
{
    long result =-1;
    Cursor c =
p_activity.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        new String[]{MediaStore.Images.Media._ID +""},
        MediaStore.Images.Media.DATA +" like '" + p_File.getAbsolutePath(),
        null, null);
    c.moveToNext();
    if (!c.isAfterLast())
    {
        result = c.getLong(0);
    }
    c.close();
    return result;
}

I'm also doing the same thing to load video thumbnails. I've used some debugging code to inspect the values returned by GetImageID(), and I'm pretty sure that they are correct.

Can anyone see what I might be doing wrong? Or know any reason why the thumbnails stop being properly generated after some time?

A: 

Hmm, your query has a 'like' in it, and you only have one '. In addition you don't protect your query against SQL injection!

I would recommend a query like this:

p_activity.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
    new String[]{ MediaStore.Images.Media._ID +"" },
    MediaStore.Images.Media.DATA + "=?",
    new String[]{ p_File.getAbsolutePath() },
    null);

Maybe that already was the problem.

mreichelt
Thanks for your answer! I had some code in there to protect againt SQL injection by replacing characters with their escaped equivalents. It was somewhat messy so I took it out to simplify the code here - Looks like I took out one of the 's. The way you have used here is better though, so I'll change my code to do that. I don't think that this will solve my problem though, as it always seemed that the ID was coming back correctly.
Auber