Android OutOfMemory Issue(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 问题
基础教程推荐
- iOS4 创建后台定时器 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
