Java tutorial
//package com.java2s; /* * Hewlett-Packard Company * All rights reserved. * * This file, its contents, concepts, methods, behavior, and operation * (collectively the "Software") are protected by trade secret, patent, * and copyright laws. The use of the Software is governed by a license * agreement. Disclosure of the Software to third parties, in any form, * in whole or in part, is expressly prohibited except as authorized by * the license agreement. */ import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class Main { private static Bitmap getImage(String imagePath) { final int MAX_TRIALS = 3; int trial = 0; Bitmap bitmap = null; do { try { bitmap = BitmapFactory.decodeFile(imagePath); } catch (OutOfMemoryError e) { System.gc(); try { Thread.sleep(1000); } catch (InterruptedException e1) { throw new RuntimeException("OoM"); } } } while (trial++ != MAX_TRIALS); if (trial == MAX_TRIALS || bitmap == null) { throw new RuntimeException("OoM"); } return bitmap; } }