Java tutorial
//package com.java2s; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import java.io.IOException; import java.io.InputStream; public class Main { public static Bitmap decodeFileFromDrawable(int id, int maxSize, Context context) { Bitmap b = null; try { // Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; InputStream inputStream = context.getResources().openRawResource(id); BitmapFactory.decodeStream(inputStream, null, o); inputStream.close(); int scale = 1; if (o.outHeight > maxSize || o.outWidth > maxSize) { scale = (int) Math.pow(2, (int) Math .round(Math.log(maxSize / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5))); } // Decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; inputStream = context.getResources().openRawResource(id); b = BitmapFactory.decodeStream(inputStream, null, o2); inputStream.close(); } catch (IOException e) { } return b; } }