Java tutorial
//package com.java2s; import android.content.Context; import android.content.res.AssetManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import java.io.IOException; import java.io.InputStream; public class Main { public static Bitmap getImageBitmapFromAssetsFolderThroughImagePathName(Context context, String imagePathName, int reqWidth, int reqHeight) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; Bitmap bitmap = null; AssetManager assetManager = context.getResources().getAssets(); InputStream inputStream = null; try { inputStream = assetManager.open(imagePathName); inputStream.mark(Integer.MAX_VALUE); } catch (IOException e) { e.printStackTrace(); return null; } catch (OutOfMemoryError e) { e.printStackTrace(); System.gc(); return null; } try { if (inputStream != null) { BitmapFactory.decodeStream(inputStream, null, opts); inputStream.reset(); } else { return null; } } catch (OutOfMemoryError e) { e.printStackTrace(); System.gc(); return null; } catch (Exception e) { e.printStackTrace(); return null; } opts.inSampleSize = calculateInSampleSiez(opts, reqWidth, reqHeight); // Log.d(TAG,""+opts.inSampleSize); opts.inJustDecodeBounds = false; opts.inPreferredConfig = Bitmap.Config.RGB_565; opts.inPurgeable = true; opts.inInputShareable = true; opts.inDither = false; opts.inTempStorage = new byte[512 * 1024]; try { if (inputStream != null) { bitmap = BitmapFactory.decodeStream(inputStream, null, opts); } else { return null; } } catch (OutOfMemoryError e) { e.printStackTrace(); System.gc(); return null; } catch (Exception e) { e.printStackTrace(); return null; } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception e) { e.printStackTrace(); return null; } } } //Log.d(TAG,"w:"+bitmap.getWidth()+" h:"+bitmap.getHeight()); if (bitmap != null) { try { bitmap = Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, true); } catch (OutOfMemoryError outOfMemoryError) { outOfMemoryError.printStackTrace(); System.gc(); return null; } } return bitmap; } public static int calculateInSampleSiez(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 and keeps both // height and width larger than the requested height and width. while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) { inSampleSize *= 2; } } return inSampleSize; } }