Get images thumbnail file paths
Asked Answered
S

1

5

I'm trying to get the thumnail paths, not the bitmaps objects.
When I query for these, some of the thumbnail paths are null for some reason. (I have 1028 thumbnail images in my device, the cursor length is indeed 1028, but still returning nulls) I know there are 1028 thumbnail images because I checked. Here's my code:

     String[] projection = {MediaStore.Images.Thumbnails._ID};
  // Create the cursor pointing to the SDCard

  cursor = this.getContentResolver().query( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
          projection, // Which columns to return
          null,       // Return all rows
          null,
          MediaStore.Images.Thumbnails.IMAGE_ID);
  // Get the column index of the Thumbnails Image ID
  Log.d(Global.TAG, "BEFORE");
  columnIndex = cursor.getColumnIndex(MediaStore.Images.Thumbnails._ID);
  Log.d(Global.TAG, "AFTER1");
  for(int i =0;i<cursor.getCount();i++){
      cursor.moveToPosition(i);

      Log.d("MyTag","BBABA" + i +" : " + getThumbnailPathForLocalFile(cursor.getLong(columnIndex)));
  }
  cursor.close();

My getThumbnailPathForLocalFile:

    String getThumbnailPathForLocalFile(long fileId)
 {
    // Log.d(Global., msg)
     Cursor thumbCursor = null;
     try
     {
         thumbCursor = this.getContentResolver().
                 query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI
                 , null
                 , MediaStore.Images.Thumbnails.IMAGE_ID + " = " + fileId+ " AND "
                   + MediaStore.Images.Thumbnails.KIND + " = "
                   + MediaStore.Images.Thumbnails.MINI_KIND , null, null);

         if(thumbCursor.moveToFirst())
         {
             // the path is stored in the DATA column
             int dataIndex = thumbCursor.getColumnIndexOrThrow( MediaStore.MediaColumns.DATA );
             String thumbnailPath = thumbCursor.getString(dataIndex);
             return thumbnailPath;
         }
     }
     finally
     {
         if(thumbCursor != null)
         {
             thumbCursor.close();
         }
     }

     return null;
 }

Here's my logcat: http://pastebin.com/UZLZF9Pg

After checking, I see that the ids I send are just like the index of the for loop. I'm not even sure that my code is supposed to work, so any other code will be great.

Subjectivism answered 11/8, 2013 at 12:49 Comment(4)
Why did you set your PROJECTION to MediaStore.Images.Thumbnails._ID and not MediaStore.Images.Thumbnails.DATA?Taxi
@Taxi I'm not sure. I think I took that example off somewhere.Do you think it may solve the problem? Anyways, it's irrelavant already.Subjectivism
I'm was asking because I used {MediaStore.Images.Thumbnails.DATA}, just like in the solution, and it worked. So I was curious if you had a different approach.Taxi
Uhh, if you wouldv'e told me that some months ago, you could solve me so many issues.. :(Subjectivism
T
11

You should query for MediaStore.Images.Thumbnails.DATA. To modify your example, it would look like this.

String[] projection = {MediaStore.Images.Thumbnails.DATA};
// Create the cursor pointing to the SDCard

    Cursor cursor = this.getContentResolver().query( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
        projection, // Which columns to return
        null,       // Return all rows
        null,
        null);
// Get the column index of the Thumbnails Image ID
Log.d(TAG, "BEFORE");
int columnIndex = cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA);
Log.d(TAG, "AFTER1");
for(int i =0;i<cursor.getCount();i++){
    cursor.moveToPosition(i);

    Log.d("MyTag","BBABA" + i +" : " + cursor.getString(columnIndex));
}
cursor.close();

Reference: How to get imagepath from thumbnail path of a image?

Theone answered 18/8, 2013 at 22:48 Comment(4)
This is great, works!!! Thank you so much, I've been looking so much for this solution :)Subjectivism
Hey, hopefully you could help me with another little question: I've just removed all my gallery images, and then took around 12 images with the camera. Afterwards, I entered the Gallery, and saw the thumbnails display nicely. However, when I entered my app to display those thumbnails, I see none. After looking the the .Thubnails folder in the DCIM, there are no jpg files at all, but only one weird file that weights 10.9 MB. Do you know why my .Thumbnails folder doesn't show my thumbnails?Subjectivism
@Subjectivism Please open that as a separate question and provide more detail ( what is the name of the 'Weird file'), also phone details and OS version.Theone
I find out that only a few images has thumbnail.I don't know whyIngamar

© 2022 - 2024 — McMap. All rights reserved.