Reputation: 1765
Image is always making problem for me always :(.
java.lang.OutOfMemoryError: bitmap size exceeds VM budget - android
is the main problem even if i use very small image.
Please suggest me some tips in using Image. (link, tutorials, sample projects etc...)
Some of my quires are
Drawable
or bitmap
BitmapFactory.decodeFile
or Drawable.createFromPath
BitmapFactory.decodeStream
or Drawable.createFromStream
Drawable
and Bitmap
while rotating. Drawable
or Bitmap
through intentUpvotes: 2
Views: 2476
Reputation: 4493
To avoid out of memory error we can use BitmapRegionDecoder ( to decode the original image which is larger than maximum texture limit 2048x2048 ) or we can decode it by specifying the coordinates from the original bitmap. (for e.g coordinates of each quarter)
Upvotes: 0
Reputation: 536
that all depends on what exactly you want to do with your image. bitmapfactory is for creating bitmaps. and drawable objects are used when you want to draw something :)
what do you want to do with your bitmap after you have created it in the memory?
as for rotating, you should take care of onRestoreInstanceState and onSaveInstanceState. something like this:
private static final String PHOTO_TAKEN = "photo_taken";
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState.getBoolean(PHOTO_TAKEN)) {
// do something if you have your pic here
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean(PHOTO_TAKEN, mPhotoTaken); // saving the state of the image (if the photo is taken or not in this case)
}
as for passing through extras... i bet it's a bad idea as you already getting outofmemory exception.... you can pass a reference to that image, like ID of path or something else.
UPDATE:
in one of the projects i was trying to show the image taken from the cam in an ImageView control ion my activity and got that outofmemory exception. the reason was that it was putting the whole big image into memory. the workaround was quite simple: i decreased the image size:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(mImagePath, options);
Upvotes: 2