内存泄漏错误的Andr​​oid [英] Memory Leak Error Android

查看:158
本文介绍了内存泄漏错误的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

InputSream backgroundBitmap;
//backgroundBitmap is initialized from asset image.png
immutableBitmap = BitmapFactory.decodeStream(backgroundBitmap);

收到此错误:

java.lang.OutOfMemoryError
在android.graphics.BitmapFactory.nativeDe $ C $检测板(本机方法)

java.lang.OutOfMemoryError at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)

帮我解决这个问题。

推荐答案

从资产试试这个..文件

try this.. file from asset

File f = new File("file:///android_assetassets/mydemo.png")

public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){
        try {
            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);

            //The new size we want to scale to
            final int REQUIRED_WIDTH=WIDTH;
            final int REQUIRED_HIGHT=HIGHT;
            //Find the correct scale value. It should be the power of 2.
            int scale=1;
            while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
                scale*=2;

            //Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        }
            catch (FileNotFoundException e) {}
        return null;
    }

当你经过的宽度和高度,此功能将扩展位图

this function will scale bitmap as you pass width and height

这篇关于内存泄漏错误的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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