Java tutorial
//package com.java2s; //License from project: Apache License import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Build; import java.io.InputStream; public class Main { public static boolean inNativeAllocAccessError = false; /** * * @param stream * @param dip * @return */ public static Bitmap decodeStream(InputStream stream, float dip) { BitmapFactory.Options options = new BitmapFactory.Options(); if (dip != 0.0F) { options.inDensity = (int) (160.0F * dip); } options.inPreferredConfig = Bitmap.Config.ARGB_8888; setInNativeAlloc(options); try { Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options); return bitmap; } catch (OutOfMemoryError e) { options.inPreferredConfig = Bitmap.Config.RGB_565; setInNativeAlloc(options); try { Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options); return bitmap; } catch (OutOfMemoryError e2) { } } return null; } public static void setInNativeAlloc(BitmapFactory.Options options) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && !inNativeAllocAccessError) { try { BitmapFactory.Options.class.getField("inNativeAlloc").setBoolean(options, true); return; } catch (Exception e) { inNativeAllocAccessError = true; } } } }