我正在创建可绘制的位图在运行时,并使它们作为我的视图在活动onResume()
事件的背景,我正在删除背景图像onStop()
事件,以删除位图引用,以便recycled.The应用程序可以在安卓2.3.5版很好,但它抛出异常安卓4.1.2版。可能的原因是什么?我还使用了来自http://developer.android.com/training/displaying-bitmaps/manage-memory.html的softReferences。我得到了下面的异常。任何帮助都将不胜感激。
10-11 14:54:48.812: E/dalvikvm-heap(1760): Out of memory on a 41508-byte allocation.
10-11 14:54:48.872: E/AndroidRuntime(1760): FATAL EXCEPTION: main
10-11 14:54:48.872: E/AndroidRuntime(1760): java.lang.OutOfMemoryError
10-11 14:54:48.872: E/AndroidRuntime(1760): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
10-11 14:54:48.872: E/AndroidRuntime(1760): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:527)
10-11 14:54:48.872: E/AndroidRuntime(1760): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:301)
发布于 2013-10-11 19:18:46
在选择文件时,使用以下函数将其转换为位图并处理结果,
public static Bitmap convertToBitmap(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=250;
//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;
bmp=BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return bmp;
}//convertToBitmap
https://stackoverflow.com/questions/19316765
复制相似问题