1
File imgFile = new  File("/sdcard/Images/test_image.jpg");

if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

    myImage.setImageBitmap(myBitmap);

}

//This works all well and good until myImage.setImageBitmap is called again (after a picture is taken and loaded in this little "post-view" ImageView rectangle which is myImage) and it runs out of memory.

Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
murphy
  • 11
  • 4
  • Have you tried disabling image caching, or invalidate() on image view? – Abhinav Saxena Nov 10 '14 at 05:03
  • you can make your custom class and notify when image changes on ImageView and then recycle the bitmap . have some idea from here http://stackoverflow.com/questions/13622081/imageview-onimagechangedlistener-android – Hradesh Kumar Nov 10 '14 at 06:40

2 Answers2

0

I think that will helps you.

File imgFile = new  File("/sdcard/Images/test_image.jpg");
private Bitmap mLastUpdatedBitmap;

if(imgFile.exists()){
    if(mLastUpdatedBitmap != null && !mLastUpdatedBitmap.isRecycled()){
       mLastUpdatedBitmap.recycle();
    }
    mLastUpdatedBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
    myImage.setImageBitmap(mLastUpdatedBitmap);

}
VikasGoyal
  • 3,308
  • 1
  • 22
  • 42
0
Bitmap myBitmap;
if(imgFile.exists()){

     if(myBitmap!=null){
         myBitmap.recycle();
         myBitmap=null;
     }
     myBitmap= BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

    myImage.setImageBitmap(myBitmap);

}
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
  • I should note of importance to prevent out of memory error I needed to consider my source of the images (the camera). My image was just way too big especially considering I had another handler on a timer that was loading and sending the picture via http at the same time (when connection was available). When I setup my camera I had to set some parameters: c = Camera.open(); Camera.Parameters p = c.getParameters(); p.setJpegQuality (75); p.setPictureSize (640,480); c.setParameters(p); – murphy Nov 13 '14 at 05:52
  • @murphy what you want to know. Can you please clarify? – Pratik Dasa Nov 13 '14 at 05:53