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;
	@Override
	  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        pic = (ImageView) findViewById(R.id.picture); // assuming your imageview id is 'picture'

		imgFile = new  File("your_path");

		if(imgFile.exists()){
			Bitmap myBitmap = decodeSampleImage(imgFile, 100, 100);  // method will be added below
			((BitmapDrawable)pic.getDrawable()).getBitmap().recycle(); // most of the time this will work alone
			pic.setImageBitmap(myBitmap);
		}
	      /** if image is clicked than open the image in a image app **/
	        pic.setOnTouchListener(new OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					Intent intent = new Intent();
					intent.setAction(Intent.ACTION_VIEW);
					intent.setDataAndType(Uri.parse("file://" +  imgFile.getAbsolutePath()), "image/*");
					startActivity(intent);
					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;
	    	}

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

  • wholesale nfl jerseys
  • wholesale nfl jerseys china
  • wholesale sports jerseys