7

I know how to get a photo from gallery in android

Intent gallery = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(gallery, PHOTO_REQUEST_CODE);

But how would I specifically select a thumbnail?

REASON FOR BOUNTY:

I have already tried both solutions at Get thumbnail Uri/path of the image stored in sd card + android . They don't work for me. I don't know how to get selectedImageUri, which is of type long, from data in

 onActivityResult(int requestCode, int resultCode, Intent data)
Community
  • 1
  • 1
Pouton Gerald
  • 1,625
  • 22
  • 32
  • 1
    Possible duplicate of [Get thumbnail Uri/path of the image stored in sd card + android](http://stackoverflow.com/questions/5548645/get-thumbnail-uri-path-of-the-image-stored-in-sd-card-android) – Miro Markaravanes Jun 04 '13 at 22:05
  • @MiroMarkarian thanks for the link. But I am getting errors when I use `Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail( getContentResolver(), data.getData(), MediaStore.Images.Thumbnails.MINI_KIND, (BitmapFactory.Options) null);`. How would I change `data.getData()` to an id as suggested in the reply? – Pouton Gerald Jun 04 '13 at 22:20
  • Try to use `Cursor`s. It's the second solution the guy over there suggested and it was reported that it worked better than the `Bitmap` option – Miro Markaravanes Jun 04 '13 at 22:27
  • @MiroMarkarian The second solution is expecting a `long` as `selectedImageUri`. But all I have is `data.getData()`. How do I get/parse the id? – Pouton Gerald Jun 10 '13 at 16:55
  • Hey, did you ever figure it out? I'm looking at the same problem... – Jona Feb 03 '15 at 00:30

3 Answers3

0
String fn = ...; // file name
ContentResolver cr = ctx.getContentResolver();
Cursor c = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
           new String[]{
              BaseColumns._ID
           }, MediaColumns.DATA + "=?", new String[]{ fn }, null);
     if(c!=null) {
        try{
           if(c.moveToNext()) {
              long id = c.getLong(0);
              Bitmap thumbnail = MediaStore.Images.Thumbnails.getThumbnail(cr, id, MediaStore.Images.Thumbnails.MINI_KIND, null);
           }
        }finally{
           c.close();
        }
     }
Pointer Null
  • 39,597
  • 13
  • 90
  • 111
0

If you have its cursor in hand, you can get its ID as ,

int id = cursor.getInt(cursor
                    .getColumnIndex(MediaStore.MediaColumns._ID));

Reffer the following code

Cursor cursor = context.getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new String[] { MediaStore.Images.Media._ID },
                MediaStore.Images.Media.DATA + "=? ",
                new String[] { filePath }, null);

        if (cursor != null && cursor.moveToFirst()) {
            int id = cursor.getInt(cursor
                    .getColumnIndex(MediaStore.MediaColumns._ID));
            Uri baseUri = Uri.parse("content://media/external/images/media");
            return Uri.withAppendedPath(baseUri, "" + id);

So, for thumbnail,

Bitmap thumbnail = MediaStore.Images.Thumbnails.getThumbnail(cursor, id, MediaStore.Images.Thumbnails.MINI_KIND, null);
Nizam
  • 5,698
  • 9
  • 45
  • 57
0

Hey so if everything else isn't working for you here is an easy way to make your own thumbnail if you have the Bitmap. If you don't know how to load the Bitmap from the Uri:

Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);

Here is the code to make a nice formatted thumbnail:

        final int THUMBNAIL_HEIGHT = 75;//48
        final int THUMBNAIL_WIDTH = 75;//66
        Float width  = new Float(bitmap.getWidth());
        Float height = new Float(bitmap.getHeight());
        Float ratio = width/height;
        bitmap = Bitmap.createScaledBitmap(bitmap, (int)(THUMBNAIL_HEIGHT*ratio), THUMBNAIL_HEIGHT, false);

        int padding = (THUMBNAIL_WIDTH - bitmap.getWidth())/2;
        image.setPadding(padding, padding, padding, padding);
        image.setBackgroundColor(0);
        image.setImageBitmap(bitmap);

In this code "image" is the variable for the ImageView. I hope this helps some :D

Cheney Hester
  • 528
  • 1
  • 5
  • 22