我正在开发使用从我的应用程序下载的图像在后台显示的应用程序。当我启动应用程序时,它工作正常并且加载图像没有任何问题。但在几次重新启动后,应用程序因内存错误而崩溃。我使用以下方法获取 Drawable 以在视图中显示它。我已经使用了一个类,并且具有这种静态方法,我将它用于所有图像。我还为清除位图调用了另一种静态方法,但不知道它是否有效。可能是我关闭应用程序时无法清除内存。
public static void recycle_bitmap() {
if (myBitmap != null) {
myBitmap.recycle();
myBitmap = null;
}
}
static Bitmap myBitmap = null;
public static Drawable ImgDrawableFromFile(Resources res, String file_name) {
myBitmap=null;
File imgFile = new File("/data/data/com.appstart/app_my_sub_dir/"
+ file_name + ".jpg");
if (imgFile.exists()) {
myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
if (myBitmap != null)
return new BitmapDrawable(res, myBitmap);
else
return null;
}
return null;
}
Logcat 错误
01-11 12:09:41.860: D/dalvikvm(6047): GC_EXTERNAL_ALLOC freed 598K, 48% free 3336K/6407K, external 8683K/10523K, paused 274ms
01-11 12:09:42.149: E/dalvikvm-heap(6047): 1671840-byte external allocation too large for this process.
01-11 12:09:42.480: I/dalvikvm-heap(6047): Clamp target GC heap from 16.094MB to 16.000MB
01-11 12:09:42.480: E/GraphicsJNI(6047): VM won't let us allocate 1671840 bytes
01-11 12:09:42.501: D/dalvikvm(6047): GC_FOR_MALLOC freed 1K, 48% free 3334K/6407K, external 8674K/10523K, paused 247ms
01-11 12:09:42.501: D/skia(6047): --- decoder->decode returned false
01-11 12:09:42.501: W/dalvikvm(6047): threadid=15: thread exiting with uncaught exception (group=0x40015560)
01-11 12:09:42.560: E/AndroidRuntime(6047): FATAL EXCEPTION: Thread-18
01-11 12:09:42.560: E/AndroidRuntime(6047): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
01-11 12:09:42.560: E/AndroidRuntime(6047): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
01-11 12:09:42.560: E/AndroidRuntime(6047): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:470)
01-11 12:09:42.560: E/AndroidRuntime(6047): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:284)
01-11 12:09:42.560: E/AndroidRuntime(6047): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:309)
01-11 12:09:42.560: E/AndroidRuntime(6047): at com.appstart.utility.LoadImage.ImgDrawableFromFile(LoadImage.java:30)
01-11 12:09:42.560: E/AndroidRuntime(6047): at com.appstart.MainActivity.run(MainActivity.java:70)
01-11 12:09:42.560: E/AndroidRuntime(6047): at java.lang.Thread.run(Thread.java:1019)