堆大小的机器人495KB摄像10MB [英] android 495KB image taking 10MB of Heap size

查看:155
本文介绍了堆大小的机器人495KB摄像10MB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图加载图像在我的应用程序有大小495KB。如果我打开这个图片不是从25MB堆大小增大到35MB,这是造成我的应用程序真正的内存问题。如果我不加载这个形象不是堆的大小保持在25MB。谁能告诉它为什么需要这么多的堆大小?

I am trying to load an image in my app having size 495KB. If I load this image than heap size increases from 25MB to 35MB which is causing real memory issues in my app. If i dont load this image than heap size stays at 25MB. Can anybody tell why it is taking so much heap size?

图片低于

Image is below

code是

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

谢谢 阿亚兹·阿拉维

Thanks Ayaz Alavi

推荐答案

图片文件时,它的加载到内存中pssed uncom $ P $。在5600×480像素,每像素32位,你的形象的作品出来的时候uncom pressed因为几乎整整10 MB。

The image file is uncompressed when it's loaded into memory. At 5600 x 480 pixels and 32 bits per pixel, your image works out as almost exactly 10 MB when uncompressed.

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

My recommendation would be to cut it into smaller sections and only load the sections you need.

这篇关于堆大小的机器人495KB摄像10MB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆