android 495KB图片占用10MB的堆大小

Aya*_*avi 1 heap android bitmap

我正在尝试在我的应用程序中加载大小为495KB的图像.如果我加载此图像比堆大小从25MB增加到35MB,这导致我的应用程序中出现真正的内存问题.如果我不加载此图像比堆大小保持在25MB.任何人都可以告诉它为什么需要这么多的堆大小?

图片如下 在此输入图像描述

我用来加载图片的代码是

    InputStream s4 = getResources().openRawResource(R.drawable.parallax_layer4);    
    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
    System.gc();
    if(bitmap4 != null) {
        bitmap4.recycle();
    }
    s3 = null;
    System.gc();     

    bitmap4 = bitmap(s4);
    layer4Back = new ImageView(this);
    layer4Back.setImageBitmap(bitmap4);
    layer4Back.setScaleType(ScaleType.FIT_XY);
    parallaxLayout.addView(layer4Back, 3, lp);
    try {
        s4.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }
    s4 = null;
    System.gc();

private static Bitmap bitmap(final InputStream is) {
    Bitmap bitmap = null;
    System.gc();
    Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inSampleSize = 1; 

    try {
       // bitmap = BitmapFactory.decodeStream(is);
       bitmap = BitmapFactory.decodeStream(is, null, options);

    } catch (Error e) {
       // TODO: handle exception
       e.printStackTrace();
    }
    return bitmap;
}
Run Code Online (Sandbox Code Playgroud)

谢谢Ayaz Alavi

gra*_*rks 5

将图像文件加载到内存中时,图像文件将被解压缩.在5600 x 480像素和每像素32位时,您的图像在未压缩时几乎完全为10 MB.

我的建议是将它切成更小的部分,只加载你需要的部分.