Java tutorial
//package com.java2s; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class Main { public static Bitmap safeDecodeBitmap(String bmpFile, BitmapFactory.Options options) { BitmapFactory.Options optsTmp = options; if (optsTmp == null) { optsTmp = new BitmapFactory.Options(); optsTmp.inSampleSize = 1; } Bitmap bmp = null; FileInputStream fis = null; int i = 1; while (i < 5) { try { fis = new FileInputStream(bmpFile); bmp = BitmapFactory.decodeStream(fis, null, optsTmp); break; } catch (OutOfMemoryError error) { error.printStackTrace(); optsTmp.inSampleSize *= 2; try { if (fis != null) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } ++i; } catch (FileNotFoundException e) { e.printStackTrace(); break; } } return bmp; } }