简体   繁体   English

将图像从 URL 加载到 bitmap 时出现内存不足

[英]OutOfMemory when load image to bitmap from URL

I load image from url.我从 url 加载图像。 it' ok, but when long time it error outofmemory: bitmap size exceeds vm budget.没关系,但是时间长了会报内存不足:bitmap 大小超出 vm 预算。 here my code这是我的代码

    BitmapFactory.Options bmOptions;
    bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    //from web
    try {
        Bitmap bitmap=null;
        InputStream is=new URL(url).openStream();
        BitmapFactory.decodeStream(is, null, bmOptions);
        is.close();
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = 1;
        is = new URL(url).openStream();
        bitmap = BitmapFactory.decodeStream(is, null, o2);
        is.close();
        return bitmap;
    } catch (Exception ex){
       ex.printStackTrace();
       return null;
    }

help me please!!请帮帮我!!

Try using the sampleSize of BitmapFactory.Options, this will reduce the size in memory of your image (and the quality)尝试使用 BitmapFactory.Options 的sampleSize ,这将减少图像的 memory 的大小(和质量)

But if your image is really too big, I think that's there no miracle solution, the image is simply too big...但是如果你的图像真的太大了,我认为那没有奇迹般的解决方案,图像太大了......

Your problem is exactly what stated in error message: your image is too big to be loaded into memory.您的问题正是错误消息中所述:您的图像太大而无法加载到 memory 中。

Try with any other image.尝试使用任何其他图像。 As this image is exceding the size of ur folder.由于此图像超出了您文件夹的大小。

R u using Emulator to test it? R 你用模拟器测试一下吗?

Just a idea, it could be this piece of code throws sometime a exception.只是一个想法,可能是这段代码有时会抛出异常。 This will causes that the InputStream will not release rthe asigned resources (memory leak).这将导致 InputStream 不会释放分配的资源(内存泄漏)。

if you close the Input stream in a finaly block this issue should be solved如果您在最后一个块中关闭输入 stream 这个问题应该得到解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM