Java tutorial
//package com.java2s; //License from project: Apache License 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 safeDecodeBimtapFile(String bmpFile, BitmapFactory.Options opts) { BitmapFactory.Options optsTmp = opts; if (optsTmp == null) { optsTmp = new BitmapFactory.Options(); optsTmp.inSampleSize = 1; } Bitmap bmp = null; FileInputStream input = null; final int MAX_TRIAL = 5; for (int i = 0; i < MAX_TRIAL; ++i) { try { input = new FileInputStream(bmpFile); bmp = BitmapFactory.decodeStream(input, null, opts); try { input.close(); } catch (IOException e) { e.printStackTrace(); } break; } catch (OutOfMemoryError e) { e.printStackTrace(); optsTmp.inSampleSize *= 2; try { input.close(); } catch (IOException e1) { e1.printStackTrace(); } } catch (FileNotFoundException e) { break; } } return bmp; } }