Question

I'm showing thumbnails for image files found on the storage (kind of a custom gallery), I'm using the following code to retrieve the thumbnails based on the file path:

Cursor cursor = mContentResolver.query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new String[] { MediaStore.MediaColumns._ID },
                MediaStore.MediaColumns.DATA + " LIKE ? ",
                new String[] { "%" + str[0] } , null);  //str[0] holds the path to the file without the first slash         

        if ( cursor.moveToFirst()) {
            //here we get the ID of the image
            int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));

            //bitmap variable holds the thumbnail for the image file we need 
            bitmap = MediaStore.Images.Thumbnails.getThumbnail(
                    getContentResolver(), id,
                    MediaStore.Images.Thumbnails.MICRO_KIND,
                    (BitmapFactory.Options) null );
        }

The code is working great in most cases, tested on a Galaxy S stock Android 2.3.3, ZTE with custom 2.3.5, Emulator for Nexus 7 with Android 4.4.2, but the code fails to work on a Galaxy S2 with custom rom (SlimKat 4.4.2 Custom Rom). I tried to debug it and discovered that the cursor has always 0 rows (normally it would return 1 row). Does anyone know why this could happen?

Was it helpful?

Solution

Got back to this question after a while. So the solution was actually to use not an absolute/relative path, but just the name of the image file. Apparently not in all versions it works the same way (and might depend on the device), so this appears to be a working solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top