Java tutorial
//package com.java2s; //License from project: Open Source License import java.io.FileNotFoundException; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; public class Main { /** Returns a Bitmap from the given URI that may be scaled by an integer factor to reduce its size, * so that its width and height are no greater than the corresponding parameters. The scale factor * will be a power of 2. */ public static Bitmap scaledBitmapFromURIWithMaximumSize(Context context, Uri imageURI, int width, int height) throws FileNotFoundException { BitmapFactory.Options options = computeBitmapSizeFromURI(context, imageURI); options.inJustDecodeBounds = false; int wratio = powerOf2GreaterOrEqual(1.0 * options.outWidth / width); int hratio = powerOf2GreaterOrEqual(1.0 * options.outHeight / height); options.inSampleSize = Math.max(wratio, hratio); return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imageURI), null, options); } /** Returns a BitmapFactory.Options object containing the size of the image at the given URI, * without actually loading the image. */ public static BitmapFactory.Options computeBitmapSizeFromURI(Context context, Uri imageURI) throws FileNotFoundException { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imageURI), null, options); return options; } static int powerOf2GreaterOrEqual(double arg) { if (arg < 0 && arg > (1 << 31)) throw new IllegalArgumentException(arg + " out of range"); int result = 1; while (result < arg) result <<= 1; return result; } }