Showing posts with label Bitmap out of memory. Show all posts
Showing posts with label Bitmap out of memory. Show all posts

Thursday, October 20, 2011

Bitmap out of memory

Image size Out of Memory you can use this code:
(android BitmapFactory.decodeStream out of memory error)

BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream fis = new FileInputStream(files[i].getAbsolutePath());
BitmapFactory.decodeStream(fis, null, o);
fis.close();
final int REQUIRED_SIZE=70;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
BitmapFactory.Options op = new BitmapFactory.Options();
op.inSampleSize = scale;
fis = new FileInputStream(files[i].getAbsolutePath());
bitmap = BitmapFactory.decodeStream(fis, null, op);
fis.close();
===============================
you can also use this :
bitmap.recycle();
after using the bitmap every time use this method so that it will free the memory and now memory will be available for next bitmap.
Example:
Bitmap bm = BitmapFactory.decodeStream(instream);

Bitmap useThisBitmap = Bitmap.createScaledBitmap(bm,bm.getWidth(),bm.getHeight(), true);
bm.recycle();
BitmapDrawable bt= new BitmapDrawable(useThisBitmap);


I am sure it will solve the out of memory problem.