Android的内存不足问题 [英] Android OutOfMemory Issue

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

问题描述

我在我的应用程序面对的内存不足问题 还有一些搜索后,我发现这个code

  //德$ C $连拍影像和扩展它来减少内存消耗
私人位图德codeFILE(文件f){
    尝试 {
        //德code图像尺寸
        BitmapFactory.Options O =新BitmapFactory.Options();
        o.inJustDe codeBounds = TRUE;
        BitmapFactory.de codeStream(新的FileInputStream(f)项,空,O);

        //我们希望新的大小扩展到
        最终诠释REQUIRED_SIZE = 70;

        //找到正确的比例值。它应该是2的幂。
        int标= 1;
        而(o.outWidth /规模/ 2> = REQUIRED_SIZE和放大器;&安培; o.outHeight /规模/ 2> = REQUIRED_SIZE)
            规模* = 2;

        //德code与inSampleSize
        BitmapFactory.Options O2 =新BitmapFactory.Options();
        o2.inSampleSize =规模;
        返回BitmapFactory.de codeStream(新的FileInputStream(f)项,空,O2);
    }赶上(FileNotFoundException异常E){}
    返回null;
}
 

但IDK如何使用它,任何帮助吗? 并且是code吧?

解决方案
  

但IDK如何使用它

那我猜你还没有实现它! 嗯,这方法对你(保存在SD卡,比如)一个调整位图至极的文件转换成可以作为一个ImageView的背景。

现在,回答你的问题,你可以使用它,因为我告诉你如下:

 位图位=去codeFILE(新文件(your_string_file_path));
myImageView.setImageBitmap(位);
 

这就是你要知道,我猜。

i face outofmemory issue in my app and after some search, I found out this code

    //decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
    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_SIZE=70;

        //Find the correct scale value. It should be the power of 2.
        int scale=1;
        while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
            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;
}

but IDK how to use it , any help please ? and is the code right ?

解决方案

but IDK how to use it

Then I guess you haven't implemented it! Well, that method will convert a file of yours (saved at SD card, eg) to a resized Bitmap wich can be used as background of an ImageView.

Now, answering to your question, you can use it as I show you below:

Bitmap bitmap = decodeFile(new File(your_string_file_path));
myImageView.setImageBitmap(bitmap);

That's all you have to know, I guess.

这篇关于Android的内存不足问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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