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))
LRUCache, even then you'll run into trouble. Use Picasso image loader, it's very easy and it'll save you a lot of headachesImageView(or whatever) that you are applying it to. UseBitmapFactory.OptionsandinSampleSizeto load something more appropriately-sized. A good library will do that for you; I think Picasso does.