I have an app which takes pictures and then displays them in the app. The first image taken works and is shown in the app, however when the second images is taken the app crashes and I get the error in the title in logcat.
p.s It is code written by a friend so I'm not 100% sure about it.
code
private PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
mImageView = (ImageView) findViewById(R.id.mImageView);
Bitmap imageBitmap = BitmapFactory.decodeByteArray(data, 0,
data.length);
mImageView.setImageBitmap(imageBitmap);
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null) {
/*
* Log.d(TAG,
* "Error creating media file, check storage permissions: " +
* e.getMessage());
*/
return;
}
try {
SharedPreferences save = getPreferences(0);
SharedPreferences.Editor editor = save.edit();
editor.putString("oldFile", pictureFile.getAbsolutePath());
// Commit the edits!
editor.commit();
Log.v("output", "oldFile: " + oldFilePath);
File oldFile = new File(oldFilePath);
if(oldFile.delete()) // DELETING PICTURES TOO FAST.
Log.v(TAG, "Image deleted.");
oldFilePath = pictureFile.getAbsolutePath();
Log.v("output", "newFile: " + oldFilePath);
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
};
logcat
02-19 14:22:08.158: E/dalvikvm-heap(10394): Out of memory on a 31961104-byte allocation. 02-19 14:22:08.163: E/AndroidRuntime(10394): FATAL EXCEPTION: main 02-19 14:22:08.163: E/AndroidRuntime(10394): java.lang.OutOfMemoryError 02-19 14:22:08.163: E/AndroidRuntime(10394): at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method) 02-19 14:22:08.163: E/AndroidRuntime(10394): at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:551) 02-19 14:22:08.163: E/AndroidRuntime(10394): at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:569) 02-19 14:22:08.163: E/AndroidRuntime(10394): at com.example.oxplastics.MainActivity$1.onPictureTaken(MainActivity.java:331) 02-19 14:22:08.163: E/AndroidRuntime(10394): at android.hardware.Camera$EventHandler.handleMessage(Camera.java:823) 02-19 14:22:08.163: E/AndroidRuntime(10394): at android.os.Handler.dispatchMessage(Handler.java:99) 02-19 14:22:08.163: E/AndroidRuntime(10394): at android.os.Looper.loop(Looper.java:137) 02-19 14:22:08.163: E/AndroidRuntime(10394): at android.app.ActivityThread.main(ActivityThread.java:4921) 02-19 14:22:08.163: E/AndroidRuntime(10394): at java.lang.reflect.Method.invokeNative(Native Method) 02-19 14:22:08.163: E/AndroidRuntime(10394): at java.lang.reflect.Method.invoke(Method.java:511) 02-19 14:22:08.163: E/AndroidRuntime(10394): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) 02-19 14:22:08.163: E/AndroidRuntime(10394): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 02-19 14:22:08.163: E/AndroidRuntime(10394): at dalvik.system.NativeStart.main(Native Method)