5

I'm getting some image from my assets folder, and I have this exception:

03-11 10:18:28.019: E/dalvikvm-heap(4052): Out of memory on a 9830416-byte allocation.

I have this error here :

//stream to get photo
InputStream bitmap=null;                        
bitmap=getResources().getAssets().open("ProduitsMini/"+productList.get(rang).getImg_mini());
Bitmap bit=BitmapFactory.decodeStream(bitmap);

// get drawable image
Drawable mDrawable = new BitmapDrawable(getResources(),bit);

It's strange because I haven't this error on each device, but only with Galaxy S3.

Jay Mayu
  • 17,023
  • 32
  • 114
  • 148
theMouk
  • 595
  • 1
  • 7
  • 21
  • Well the S3 has a xhdpi resolution, and since Android does density-dependent loading it might be related to that. On the other hand, you don't seem to use that feature the way you're loading the image. – Aert Mar 11 '13 at 10:39

1 Answers1

9

You can try adding below code

InputStream bitmap=null; 
bitmap=getResources().getAssets().open("ProduitsMini/"+productList.get(rang).getImg_mini());

BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap = BitmapFactory.decodeStream(bitmap,null,options);

This inSampleSize option reduces memory consumption.

You can refer to below link

https://stackoverflow.com/a/823966/1441666

Community
  • 1
  • 1
Nirali
  • 13,571
  • 6
  • 40
  • 53