【发布时间】:2014-02-19 14:24:28
【问题描述】:
我有一个应用程序,它可以拍照然后在应用程序中显示它们。拍摄的第一张图像有效并显示在应用程序中,但是当拍摄第二张图像时,应用程序崩溃并且我在 logcat 的标题中收到错误。
p.s 这是朋友写的代码,所以我不是 100% 确定。
代码
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());
}
}
};
日志猫
02-19 14:22:08.158:E/dalvikvm-heap(10394):31961104 字节分配内存不足。 02-19 14:22:08.163:E/AndroidRuntime(10394):致命异常:主要 02-19 14:22:08.163: E/AndroidRuntime(10394): java.lang.OutOfMemoryError 02-19 14:22:08.163: E/AndroidRuntime(10394): 在 android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method) 02-19 14:22:08.163: E/AndroidRuntime(10394): 在 android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:551) 02-19 14:22:08.163: E/AndroidRuntime(10394): 在 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): 在 android.hardware.Camera$EventHandler.handleMessage(Camera.java:823) 02-19 14:22:08.163: E/AndroidRuntime(10394): 在 android.os.Handler.dispatchMessage(Handler.java:99) 02-19 14:22:08.163: E/AndroidRuntime(10394): 在 android.os.Looper.loop(Looper.java:137) 02-19 14:22:08.163: E/AndroidRuntime(10394): 在 android.app.ActivityThread.main(ActivityThread.java:4921) 02-19 14:22:08.163: E/AndroidRuntime(10394): 在 java.lang.reflect.Method.invokeNative(Native Method) 02-19 14:22:08.163: E/AndroidRuntime(10394): 在 java.lang.reflect.Method.invoke(Method.java:511) 02-19 14:22:08.163: E/AndroidRuntime(10394): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) 02-19 14:22:08.163: E/AndroidRuntime(10394): 在 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)
【问题讨论】:
-
也发布您的代码!
-
天哪,这个问题需要多少次重复才能让人们开始查看其他问题并停止发布与位图相关的 OOM?
标签: android image memory android-image