开发者

Efficient way of using images in android (`Drawable` of `bitmap` )

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

  1. Which should i prefer Drawable or bitmap
  2. BitmapFactory.decodeFile or Drawable.createFromPath
  3. BitmapFactory.decodeStream or Drawable.createFromStream
  4. How to retain Drawab开发者_如何学Cle and Bitmap while rotating.
  5. Is it a good idea to save or pass Drawable or Bitmap through intent


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);


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜