Android: Get thumbnail of image on SD card, given Uri of original image

后端 未结 6 1447
感动是毒
感动是毒 2020-11-29 04:24

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

6条回答
  •  悲&欢浪女
    2020-11-29 04:44

    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);
    }
    

提交回复
热议问题