加载照片时位图 OutOfMemoryException
Bitmap OutOfMemoryException when loading photos
在我的应用程序中,用户可以启动相机意图并拍照并将其保存到自定义文件夹中。然后他们可以重新启动相机拍摄另一张照片等。因此最终用户可以拍摄多张照片。
我 运行 遇到的问题是,在我的图库中,当我尝试将所有图片加载到位图以放置在我的图像视图中时,出现 java.lang.OutOfMemoryError
异常。我已经阅读了一些有关内存管理的内容,但我认为我已经弄明白了。这是我的代码到目前为止的样子:
File folder = new File(dir + "/");//contains anything from 1-20 pictures
File[] allPics = folder.listFiles();
private Bitmap[] thumbnails;
private String[] arrPath;
//load all the files into bitmaps to assign to my gridview adapter
for (int i = 0; i < allPics.length; i++) {
thumbnails[i] = BitmapFactory.decodeFile(allPics[i].getAbsolutePath());//where the exception is thrown
arrPath[i]= allPics[i].getAbsolutePath();
}
堆栈跟踪:
pcemobileapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: pcemobileapp, PID: 7069
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:719)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:695)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:452)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:491)
at pcemobileapp.CustomGalleryActivity.onCreate(CustomGalleryActivity.java:65)
at android.app.Activity.performCreate(Activity.java:5459)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2458)
at android.app.ActivityThread.access0(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5598)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method))
这里很简单。您正在一次解码一堆照片。不要那样做。如果数字很高,即使是高端设备也没有足够的内存。相反,保存位图的位置,并且只在 getView 中膨胀位图。如果这不够高效,您可以使用 LRU 缓存并将最后 N 个位图存储在内存中。但是你不能只膨胀大量图像。
在我的应用程序中,用户可以启动相机意图并拍照并将其保存到自定义文件夹中。然后他们可以重新启动相机拍摄另一张照片等。因此最终用户可以拍摄多张照片。
我 运行 遇到的问题是,在我的图库中,当我尝试将所有图片加载到位图以放置在我的图像视图中时,出现 java.lang.OutOfMemoryError
异常。我已经阅读了一些有关内存管理的内容,但我认为我已经弄明白了。这是我的代码到目前为止的样子:
File folder = new File(dir + "/");//contains anything from 1-20 pictures
File[] allPics = folder.listFiles();
private Bitmap[] thumbnails;
private String[] arrPath;
//load all the files into bitmaps to assign to my gridview adapter
for (int i = 0; i < allPics.length; i++) {
thumbnails[i] = BitmapFactory.decodeFile(allPics[i].getAbsolutePath());//where the exception is thrown
arrPath[i]= allPics[i].getAbsolutePath();
}
堆栈跟踪:
pcemobileapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: pcemobileapp, PID: 7069
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:719)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:695)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:452)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:491)
at pcemobileapp.CustomGalleryActivity.onCreate(CustomGalleryActivity.java:65)
at android.app.Activity.performCreate(Activity.java:5459)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2458)
at android.app.ActivityThread.access0(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5598)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method))
这里很简单。您正在一次解码一堆照片。不要那样做。如果数字很高,即使是高端设备也没有足够的内存。相反,保存位图的位置,并且只在 getView 中膨胀位图。如果这不够高效,您可以使用 LRU 缓存并将最后 N 个位图存储在内存中。但是你不能只膨胀大量图像。