5

I am trying to download an 1mb image file and then save to bitmap to populate an imageview. but shows error

06-13 13:39:48.149: ERROR/AndroidRuntime(1782):
java.lang.OutOfMemoryError: bitmap size exceeds VM budget

How can I download a large image?

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
Jyosna
  • 4,436
  • 13
  • 60
  • 93
  • Duplicate: http://stackoverflow.com/questions/1949066/java-lang-outofmemoryerror-bitmap-size-exceeds-vm-budget-android – razlebe Jun 13 '11 at 09:41

1 Answers1

3

I had been through the same issue. Before you set the downloaded bitmap to your ImageView, you have to compress your bitmap according to the width and height of your ImageView.

You have to create a scaled bitmap like this,

 bm1=Bitmap.createScaledBitmap(bm, 300, 300,true);
imgView.setImageBitmap(bm1);

or Compress your bitmap like this,

            OutputStream fOut = null;
                        File file = new File(strDirectoy,imgname);
                            fOut = new FileOutputStream(file);

                        bm1.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                            fOut.flush();
                            fOut.close();

                MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • bm1=Bitmap.createScaledBitmap(bm, 300, 300,true); imgView.setImageBitmap(bm1); sometimes it working and sometimes its not working. Can u tell me why? – Jyosna Jun 16 '11 at 12:29
  • If not try the next method I have given. This will compress and store your image. After that try to set image for imageview using the uri. – Andro Selva Jun 16 '11 at 12:41