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; import android.util.Log; import java.io.File; import java.io.IOException; public class Main { private static final String TAG = "ImageLoaderUtil"; /** * This method gives you the bitmap of the image you tell it to load from the filesystem. * @param uri The file location. * @return The bitmap of the photo at the location you specify. */ public static Bitmap getImageBitmap(String uri) { final File f = new File(uri); try { return getImage(f.getCanonicalPath()); } catch (IOException e) { Log.e(TAG, "Error loading image for loading", e); return null; } } 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; } }