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 问题
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01