Reputation: 4158
In my application the user can launch a camera intent and take a picture and save it to a custom folder. They can then relaunch the camera to take a another picture etc. So in the end the user can take multiple pictures.
The problem I'm running into, is that in my gallery when I try and load all the pictures to bitmaps to place in my imageview's, I get a java.lang.OutOfMemoryError
exception. I've read up somewhat about memory management but I thought I had it figured out. This is what my code looks like thus far:
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();
}
stacktrace:
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.access$900(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))
Upvotes: 0
Views: 48
Reputation: 93561
Its pretty simple here. You're decoding a bunch of photos all at once. Don't do that. You don't have enough memory on even a high end device if the number is high. Instead, save the location of the bitmap, and only inflate the bitmap in getView. If that isn't performant enough you can use an LRU cache and store the last N bitmaps in memory. But you can't just inflate a huge number of images.
Upvotes: 2