Reducing image size

后端 未结 3 645
野性不改
野性不改 2021-01-28 06:30

I am trying to reduce the size of an image that a user has selected from the Gallery before passing it to another intent.

I am currently using the following code, but i

3条回答
  •  攒了一身酷
    2021-01-28 07:19

    You can use this code ...

    public static Bitmap getThumbnailBitmap(final String path,
            final int thumbnailSize) {
        Bitmap bitmap;
        BitmapFactory.Options bounds = new BitmapFactory.Options();
        bounds.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, bounds);
        if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) {
            bitmap = null;
        }
        int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
                : bounds.outWidth;
        BitmapFactory.Options opts = new BitmapFactory.Options();
        if (thumbnailSize > 0) {
            opts.inSampleSize = originalSize / thumbnailSize;
        } else {
            opts.inSampleSize = originalSize;
        }
        try {
            bitmap = BitmapFactory.decodeFile(path, opts);
    
        } catch (Exception ex) {
            return null;
        }
        return bitmap;
    }
    

提交回复
热议问题