How to change ImageView using a file path without getting out of memory exception error (Android)

This code is going be a mix of my own code and a method I took from StackOverFlow to prevent the memory exception error.  I tried many routes to solve the out of memory error but this one the only one worked for me. I think I had such a hard time because I was loading these few megabit images fairly fast. Not to go off course but check out my new Android app that I used this code for. I also will add code to open the image in an image viewer using intent. Here is the code:

Code in the onCreate:

   File imgFile;
	ImageView pic;
	  protected void onCreate(Bundle savedInstanceState) {

        pic = (ImageView) findViewById(; // assuming your imageview id is 'picture'

		imgFile = new  File("your_path");

			Bitmap myBitmap = decodeSampleImage(imgFile, 100, 100);  // method will be added below
			((BitmapDrawable)pic.getDrawable()).getBitmap().recycle(); // most of the time this will work alone
	      /** if image is clicked than open the image in a image app **/
	        pic.setOnTouchListener(new OnTouchListener() {
				public boolean onTouch(View v, MotionEvent event) {
					Intent intent = new Intent();
					intent.setDataAndType(Uri.parse("file://" +  imgFile.getAbsolutePath()), "image/*");
					return false;

decodeSampleImage() method (thanks to the person on StackOverFlow):

public static Bitmap decodeSampleImage(File f, int width, int height) {
	    	    try {
	    	        System.gc(); // First of all free some memory

	    	        // Decode image size

	    	        BitmapFactory.Options o = new BitmapFactory.Options();
	    	        o.inJustDecodeBounds = true;
	    	        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

	    	        // The new size we want to scale to

	    	        final int requiredWidth = width;
	    	        final int requiredHeight = height;

	    	        // Find the scale value (as a power of 2)

	    	        int sampleScaleSize = 1;

	    	        while (o.outWidth / sampleScaleSize / 2 >= requiredWidth && o.outHeight / sampleScaleSize / 2 >= requiredHeight)
	    	            sampleScaleSize *= 2;

	    	        // Decode with inSampleSize

	    	        BitmapFactory.Options o2 = new BitmapFactory.Options();
	    	        o2.inSampleSize = sampleScaleSize;

	    	        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
	    	    } catch (Exception e) {
	    	      //  Log.d(TAG, e.getMessage()); // We don't want the application to just throw an exception

	    	    return null;

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Graveyard Database Logo

Has everything you want to know about grave yards. Plus has many celebrites and world leaders graves. Pssst it is my other site

Google + Profile
Check out Android Photo Mapping, its a app/site I been working on
Fabulou wholesale Petr Mrazek jersey is back. Best choice to get the wholesale Petr Mrazek jersey is suitable for all style of people.