Android external allocation too large, but why?

与世无争的帅哥 提交于 2019-12-23 03:24:13

问题


I have a large image 1.28 MB (1,343,488 bytes) that I use in my app, however I wanted to change a few things and created a new image that was instead only 292 KB (299,008 bytes) in size. I created both images in Photoshop 7.0, saved them both as .png and both as interlaced. I have been creating this app around the larger image, and as soon as I switch to the smaller image, I get an OutOfMemory error with my bitmap saying

12108912-byte external allocation too large for this process

why does it think my new image is 11.5 MB? if i switch images back to the larger one, it compiles correctly.

private Bitmap loadMapBitmap() {
    Log.d("InteractiveMap", "loadBitmap");
    Bitmap bitmap = null;
    try {   
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.map);
    } catch (Resources.NotFoundException e) {
        Log.d(TAG, "loadMapBitmap - " + e.toString());
        finish();
    } catch (OutOfMemoryError e) {
        Log.d("InteractiveMap", "too big " + e.getMessage());
        finish();
    }

    return bitmap;
}

I dont get why the much smaller image doesnt work.


回答1:


It's the image resolution. The file size is not relevant. When you load a bitmap into memory, each pixel takes 4 bytes when using the ARGB8888 format
(which is the default one for BitmapFactory.decode..()).

Example: A 640x480 pixel sized image takes 640*480*4 bytes, which is 1200kB in memory. This is completely independend from the file-format used to compress this image (PNG, JPEG, ..) and filesize.

In extreme cases these sizes differ a lot. E.g. you create a huge image which consists of one color. That's pretty great when it comes to file compression. But in memory it will take a lot of space.



来源:https://stackoverflow.com/questions/7493095/android-external-allocation-too-large-but-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!