gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:33:17 36 4
gpt4 key购买 nike

我正在尝试在我的应用程序中加载大小为 495KB 的图像。如果我加载此图像,堆大小会从 25MB 增加到 35MB,这会导致我的应用程序出现实际内存问题。如果我不加载此图像,那么堆大小将保持在 25MB。任何人都可以告诉为什么它占用如此大的堆大小吗?

图片如下 enter image description here

我用来加载图像的代码是

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

谢谢阿亚兹·阿拉维

最佳答案

图像文件在加载到内存中时未压缩。在 5600 x 480 像素和每像素 32 位的情况下,您的图像在未压缩时几乎正好是 10 MB。

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

关于android 495KB 图像占用 10MB 的堆大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11357483/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com