我有一个应用程序可以拍照,然后在应用程序中显示它们。第一个拍摄的图像可以在应用程序中显示,但是当拍摄第二张图像时,应用程序崩溃了,我在logcat中得到了标题中的错误。
p.s这是由朋友写的代码所以我不是百分之百确定它。
码
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):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方法) 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):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)
答案 0 :(得分:3)
您应该在将图像显示在屏幕上之前对图像进行采样,请查看以下链接
http://developer.android.com/training/displaying-bitmaps/index.html
现在有可能第一次创建位图对象时,你的设备有足够的内存,但是当下一个对象被创建时,它会耗尽内存!!