12

So i've got a Uri of an image the user chooses out of images off his SD card. And i'd like to display a thumbnail of that image, because obviously, the image could be huge and take up the whole screen. Anyone know how?

Rahat Ahmed
  • 2,191
  • 2
  • 30
  • 40

6 Answers6

10

You can simply create thumbnail video and image using ThumnailUtil class of java

Bitmap resized = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(file.getPath()), width, height);


 public static Bitmap createVideoThumbnail (String filePath, int kind)

Added in API level 8 Create a video thumbnail for a video. May return null if the video is corrupt or the format is not supported.

Parameters filePath the path of video file kind could be MINI_KIND or MICRO_KIND

For more Source code of Thumbnail Util class

Developer.android.com

sujith s
  • 864
  • 11
  • 20
6

This code will do the job:

Bitmap getPreview(URI uri) {
    File image = new File(uri);

    BitmapFactory.Options bounds = new BitmapFactory.Options();
    bounds.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(image.getPath(), bounds);
    if ((bounds.outWidth == -1) || (bounds.outHeight == -1))
        return null;

    int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
            : bounds.outWidth;

    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = originalSize / THUMBNAIL_SIZE;
    return BitmapFactory.decodeFile(image.getPath(), opts);     
}

You may want to calculate the nearest power of 2 to use for inSampleSize, because it's said to be faster.

Gubbel
  • 2,327
  • 1
  • 24
  • 35
  • 6
    what is THUMBNAIL SIZE – Cyph3rCod3r Oct 17 '13 at 04:35
  • 1
    The constructor File(Uri) is undefined – hasanghaforian Apr 22 '15 at 16:49
  • a power of 2 for inSampleSize is not faster but required by the algorithm. Note that images may be much lager than THUMBNAIL_SIZE because if for example `inSampleSize = originalSize / THUMBNAIL_SIZE` is 7, `inSampleSize` will be rounded down to 4 giving you a size of originalSize/4, which is 1,75 times the size you expect per dimension, i.e. 3 times the pixels. This effect increases rapidly for larger sampling sizes – Simon Warta Apr 15 '17 at 11:25
2

I believe this code is fastest way to generate thumbnail from file on SD card:

 public static Bitmap decodeFile(String file, int size) {
    //Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(file, o);

    //Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = (int)Maths.pow(2, (double)(scale-1));
    while (true) {
        if (width_tmp / 2 < size || height_tmp / 2 < size) {
            break;
        }
        width_tmp /= 2;
        height_tmp /= 2;
        scale++;
    }

    //Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    return BitmapFactory.decodeFile(file, o2);
}
David Vávra
  • 18,446
  • 7
  • 48
  • 56
  • 1
    Destil your scale is underated, it's 2(scale-1). In Java: (int)Maths.pow(2, (double)(scale-1)) –  Oct 23 '12 at 07:58
  • 1
    I think you broke this code in your edit, it's using scale in it's own assignment. Also there's no such package as "Maths". – Mark Feldman Aug 09 '13 at 04:21
1

A few trying I could not get the thumbnail path of image from SD. i am resolved this problem getting an android image bitmap, before I create an image view in adapter for gridview (or where you need). So i call method imageView.setImageBitmap(someMethod(Context context, imageID))

Bitmap someMethod(Context context, long imageId){

    Bitmap bitmap =   Media.Images.Thumbnails.getThumbnail(context.getAplicationContext.getContentResolver(), imageid, MediaStore.Images.Thumbnails.MINI_KIND, null);
    return bitmap;
}

You can get image ID from your SD using this guide (Get list of photo galleries on Android)

Community
  • 1
  • 1
IHAFURR
  • 111
  • 4
0

If you like HQ thumbnails, so use [RapidDecoder][1] library. It is simple as follow:

import rapid.decoder.BitmapDecoder;
...
Bitmap bitmap = BitmapDecoder.from(getResources(), R.drawable.image)
                             .scale(width, height)
                             .useBuiltInDecoder(true)
                             .decode();

Don't forget to use builtin decoder if you want to scale down less than 50% and a HQ result. I tested it in API Level 8 :)

S.M.Mousavi
  • 5,013
  • 7
  • 44
  • 59
0

This package will let you access image URI to receive image size, large Bitmap data, sampling image to any smaller size for saving memory and maximize performance.

It uses InputStream and BitmapFactory:

public int[] getImageSize(Uri uri){
    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        InputStream input = this.getContentResolver().openInputStream(uri);
        BitmapFactory.decodeStream(input, null, options); input.close();
        return new int[]{options.outWidth, options.outHeight};
    }
    catch (Exception e){}
    return new int[]{0,0};
}
public Bitmap BitmapImage(Uri uri){return BitmapImage(uri,-1,-1);}
public Bitmap BitmapImage(Uri uri, int maxSize){return BitmapImage(uri,maxSize,maxSize);}
public Bitmap BitmapImage(Uri uri, int Wmax, int Hmax){
    try {   
        InputStream input = this.getContentResolver().openInputStream(uri);
        double ratio=1;
        if ((Wmax>-1)&&(Hmax>-1)){
            int[] wh=getImageSize(uri); double w=wh[0], h=wh[1];
            if (w/Wmax>1){ratio=Wmax/w; if (h*ratio>Hmax){ratio=Hmax/h;}}
            else if (h/Hmax>1){ratio=Hmax/h;}
        }
        BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
        bitmapOptions.inSampleSize = (int)Math.ceil(1/ratio);
        bitmapOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
        input.close();
        return bitmap;
    }
    catch (Exception e){}
    return null;
}

Four functions for different use case:

/*
    getImageSize(Uri uri): return int[]{ width, height}
    BitmapImage(Uri uri): return Bitmap in full size
    BitmapImage(Uri uri, int maxSize): return sampled Bitmap which is limited in square size
    BitmapImage(Uri uri, int Wmax, int Hmax): return sampled Bitmap which is limited width and height
*/
phnghue
  • 1,578
  • 2
  • 10
  • 9