26

SDK version - 1.6

I am using following intent to open android's default gallery:

Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(
                        Intent.createChooser(intent, "Select Picture"), 101);

Now in onActivityResult, i am able to get the original Uri and path of the selected image, but i am not able to get the Uri and path of the thumbnail of selected image.

Code for getting the original image Uri and path:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        try {

            if (requestCode == 101 && data != null) {

                Uri selectedImageUri = data.getData();
                String selectedImagePath = getPath(selectedImageUri);
} else {
                Toast toast = Toast.makeText(this, "No Image is selected.",
                        Toast.LENGTH_LONG);
                toast.show();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

PS: 1) i am not looking to resize image like this question. I am specifically looking for the thumbnails which are generated by android OS itself.

2) Using SDK version 1.6 so not interested in ThumbnailUtils class.

Community
  • 1
  • 1
mudit
  • 25,306
  • 32
  • 90
  • 132

9 Answers9

86

You can use this to get the thumbnail:

Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(
                             getContentResolver(), selectedImageUri,
                             MediaStore.Images.Thumbnails.MINI_KIND,
                             (BitmapFactory.Options) null );

There are two types of thumbnails available:
MINI_KIND: 512 x 384 thumbnail
MICRO_KIND: 96 x 96 thumbnail

OR use [queryMiniThumbnails][1] with almost same parameters to get the path of the thumbnail.

EDIT

Cursor cursor = MediaStore.Images.Thumbnails.queryMiniThumbnails(
                             getContentResolver(), selectedImageUri,
                             MediaStore.Images.Thumbnails.MINI_KIND,
                             null );
if( cursor != null && cursor.getCount() > 0 ) {
     cursor.moveToFirst();//**EDIT**
     String uri = cursor.getString( cursor.getColumnIndex( MediaStore.Images.Thumbnails.DATA ) );
}

HTH !

[1]: https://developer.android.com/reference/android/provider/MediaStore.Images.Thumbnails.html#queryMiniThumbnails(android.content.ContentResolver, android.net.Uri, int, java.lang.String[])

Tony
  • 7,767
  • 2
  • 22
  • 51
Karan
  • 12,724
  • 6
  • 40
  • 33
  • 1
    @karan: i tried using getThumbnail but it is showing error in eclipse with a message that it is undefined for "MediaStore.Images.Thumbnails" but i checked in developer docs, it is there. Any idea why this is happening? And how to get the URi of the thumbnail when using "queryMiniThumbnail". – mudit Apr 13 '11 at 05:39
  • @mudit: Make sure that you are importing "android.provider.MediaStore". I've added the queryMiniThumbnail example in answer. – Karan Apr 13 '11 at 06:15
  • @karan: i am importing "android.provider.MediaStore", but it is still giving a red line below "getThumbnail" function. Plus as in your "queryMiniThumbnail" code it is also giving error: that it is not applicable for this. So i changed it to "queryMiniThumbnails" and it compiled the code. – mudit Apr 13 '11 at 08:41
  • 6
    But after running, it gives following error :"04-13 14:04:53.781: WARN/System.err(4703): android.database.sqlite.SQLiteException: no such column: kind: , while compiling: SELECT _id, _data, _size, _display_name, title, date_added, date_modified, mime_type, description, picasa_id, isprivate, latitude, longitude, datetaken, orientation, mini_thumb_magic, bucket_id, bucket_display_name FROM images WHERE (_id = 52) AND (kind = 1) ORDER BY image_id ASC" any idea y? – mudit Apr 13 '11 at 08:41
  • Sorry queryMiniThumbanils was a typo. Btw Its working fine for me. Check this thread for more information http://www.mail-archive.com/android-developers@googlegroups.com/msg49985.html – Karan Apr 13 '11 at 09:05
  • 3
    @mudit Sorry I checked the answer and found that we are using URI where id in long format is required. Please parse the id from URI and use it wherever we are using URI (check with docs for more details). – Karan Apr 13 '11 at 11:07
  • 2
    @Karan another method "queryMiniThumbnails" need uri not id,but still get error:"android.database.sqlite.SQLiteException: no such column: kind.." – wangqi060934 Jun 10 '15 at 04:05
12

This solution is work on me!

final int THUMBSIZE = 128;

Bitmap thumbImage = ThumbnailUtils.extractThumbnail(
                         BitmapFactory.decodeFile(file.getAbsolutePath()), 
                         THUMBSIZE, 
                         THUMBSIZE);
Community
  • 1
  • 1
Mete
  • 2,805
  • 1
  • 28
  • 38
8

It could be a alternative ways as other had already mentioned in their answer but Easy way i found to get thumbnail is using ExifInterface

ExifInterface exif = new ExifInterface(pictureFile.getPath());
byte[] imageData=exif.getThumbnail();
Bitmap  thumbnail= BitmapFactory.decodeByteArray(imageData,0,imageData.length);
dharmendra
  • 7,835
  • 5
  • 38
  • 71
5

Two variants without depricated methods.

 public String getThumbnailPath(Uri uri) {
    String[] proj = { MediaStore.Images.Media.DATA };

    // This method was deprecated in API level 11
    // Cursor cursor = managedQuery(contentUri, proj, null, null, null);

    CursorLoader cursorLoader = new CursorLoader(activity, uri, proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();

    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);

    cursor.moveToFirst();
    long imageId = cursor.getLong(column_index);
    //cursor.close();
    String result="";
    cursor = MediaStore.Images.Thumbnails.queryMiniThumbnail(activity.getContentResolver(), imageId,
            MediaStore.Images.Thumbnails.MINI_KIND, null);
    if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        result = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
        cursor.close();
    }
    return result;
}
public Bitmap getThumbnailBitmap(Uri uri){
    String[] proj = { MediaStore.Images.Media.DATA };

    // This method was deprecated in API level 11
    // Cursor cursor = managedQuery(contentUri, proj, null, null, null);

    CursorLoader cursorLoader = new CursorLoader(activity, uri, proj, null, null, null);
    Cursor cursor = cursorLoader.loadInBackground();

    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);

    cursor.moveToFirst();
    long imageId = cursor.getLong(column_index);
    //cursor.close();

    Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(
            getContentResolver(), imageId,
            MediaStore.Images.Thumbnails.MINI_KIND,
            (BitmapFactory.Options) null );

    return bitmap;
}
uberchilly
  • 159
  • 1
  • 7
4

Based on @Karan's answer and following comments, just for the people that arrive here (like I did) and need a ready-to-work code:

public String getThumbnailPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media._ID };
    String result = null;
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media._ID);

    cursor.moveToFirst();
    long imageId = cursor.getLong(column_index);
    cursor.close();

    cursor = MediaStore.Images.Thumbnails.queryMiniThumbnail(
            getContentResolver(), imageId,
            MediaStore.Images.Thumbnails.MINI_KIND,
            null);
    if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        result = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
        cursor.close();
    }
    return result;
}
Juan Sánchez
  • 980
  • 2
  • 10
  • 26
  • 1
    how to deal with the "column _id does not exist" error here? i've read a few posts about it, but we are not creating a SQL database ourselves here... – jesses.co.tt Jun 18 '14 at 01:55
3
public static String getThumbnailPath(Context context, String path)
{
  long imageId = -1;

  String[] projection = new String[] { MediaStore.MediaColumns._ID };
  String selection = MediaStore.MediaColumns.DATA + "=?";
  String[] selectionArgs = new String[] { path };
  Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs, null);
  if (cursor != null && cursor.moveToFirst())
  {
    imageId = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
    cursor.close();
  }

  String result = null;
  cursor = MediaStore.Images.Thumbnails.queryMiniThumbnail(context.getContentResolver(), imageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
  if (cursor != null && cursor.getCount() > 0)
  {
    cursor.moveToFirst();
    result = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
    cursor.close();
  }

  return result;
}
Hun
  • 3,652
  • 35
  • 72
2

The accepted answer is not working for me. I use following method to make it:

    try{
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getActivity().getContentResolver(), uri);
        Bitmap thumbBitmap = ThumbnailUtils.extractThumbnail(bitmap,120,120);
        // imageView.setImageBitmap(thumbBitmap);
    }
    catch (IOException ex){
        //......
    }
Yun CHEN
  • 6,450
  • 3
  • 30
  • 33
  • this is the only approach work for me as well. But not so sure about efficiency though because it's basically creating thumbnail from a given bitmap instead of retrieving existing one. – KMC Nov 06 '19 at 04:24
2

Take a look at the following class

http://developer.android.com/reference/android/provider/MediaStore.Images.Thumbnails.html

Josnidhin
  • 12,469
  • 9
  • 42
  • 61
-1

The best Answer for getting Thumbnail and All Android Versions is this:

val thumbnail: Bitmap = if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)) {
    mContentResolver.loadThumbnail(contentUri, Size.parseSize(""), null)
} else {
    MediaStore.Images.Thumbnails.getThumbnail(mContentResolver, id, MediaStore.Images.Thumbnails.MINI_KIND, null)
}
Nafis Kabbo
  • 568
  • 5
  • 8