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 问题


- android 4中的android RadioButton问题 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01