Android OutOfMemory 问题

Android OutOfMemory Issue(Android OutOfMemory 问题)

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

问题描述

我在我的应用程序中遇到内存不足的问题经过一番搜索,我发现了这段代码

//解码图像并对其进行缩放以减少内存消耗私有位图解码文件(文件 f){尝试 {//解码图像大小BitmapFactory.Options o = new BitmapFactory.Options();o.inJustDecodeBounds = true;BitmapFactory.decodeStream(new FileInputStream(f),null,o);//我们要缩放到的新尺寸最终 int REQUIRED_SIZE=70;//找到正确的比例值.应该是2的幂.整数比例=1;while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)规模*=2;//使用 inSampleSize 解码BitmapFactory.Options o2 = new BitmapFactory.Options();o2.inSampleSize=规模;return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);} 捕捉(FileNotFoundException e){}返回空值;}

但是IDK如何使用它,请帮助?代码对吗?

解决方案

但是IDK如何使用它

那我猜你还没有实现它!好吧,该方法会将您的文件(例如保存在 SD 卡中)转换为可用作 ImageView 背景的调整大小的位图.

现在,回答您的问题,您可以使用它,如下所示:

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

我猜你要知道的就这些了.<​​/p>

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 OutOfMemory 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android OutOfMemory 问题

基础教程推荐