我在运行时创建位图可绘制并将它们作为我在活动onResume()
事件中的视图的背景,我将删除背景图像onStop()
事件,以删除位图引用,以便位图可以是再循环。应用程序适用于Android版本2.3.5但它在Android 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)
答案 0 :(得分:1)
选择文件时使用以下功能将其转换为位图并使用结果,,
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