开发者

android OutOfMemoryError on WVGA800 and not any small screen format

开发者 https://www.devze.com 2023-04-07 03:35 出处:网络
When I try and decode a bitmap on an emulator running less than WVGA800 it works fine (phones included) but on larger screens it throws a OutOfMemoryError

When I try and decode a bitmap on an emulator running less than WVGA800 it works fine (phones included) but on larger screens it throws a OutOfMemoryError

Why would that be? would phones with larger screens have more memory?

private Bitmap getBitmap(int开发者_运维问答 assetKey) { return BitmapFactory.decodeResource(mContext.getResources(), assetKey); }


Phones with larger displays don't always have more memory than phones will smaller displas. Decoded bitmaps take a lot of of memory, 4 bytes of memory per pixel.

In general it is a good idea to downsample the bitmap if they are too large. You can do this easily:

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = n; // <-- this only decode every nth pixel

Bitmap b = BitmapFactory.decodeResource(mContext, rId, ops);


It's generally a good practice to break the data in to samples, to avoid memory exception. you can see the following link for that. you have to use sampleSize more than 1. I have resolved my issues by following this post.

Strange out of memory issue while loading an image to a Bitmap object

0

精彩评论

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