Java tutorial
//package com.java2s; import java.io.InputStream; import android.content.ContentResolver; import android.content.Context; import android.graphics.BitmapFactory; import android.net.Uri; public class Main { /** @see http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue */ public static int getSampleSize(Context context, Uri uri, float maxSize) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) { try { InputStream stream = context.getContentResolver().openInputStream(uri); BitmapFactory.decodeStream(stream, null, options); stream.close(); } catch (Exception e) { e.printStackTrace(); } } else BitmapFactory.decodeFile(uri.getPath(), options); int longSide = Math.max(options.outHeight, options.outWidth); return getSampleSize(longSide, maxSize); } public static int getSampleSize(int size, float maxSize) { int scale = 1; if (size > maxSize) scale = 1 << (int) (Math.log(size / maxSize) / Math.log(2)); System.out.println("inSampleSize=" + scale); return scale; } }