我正在尝试编写一个自定义相机,让它一切正常,除了一件事。我试图模仿“本机”功能,在您拍照后,它会向您显示预览,您可以选择接受、重做或取消。我有这个工作。
问题是Camera.takePicture回调在照片拍摄后立即发生,然后我显示此“确认”屏幕。此时图像可供我使用,但我还没有准备好......
我想将 byte[] 数据存储在一个临时位置,直到用户触摸“确认,我想使用这张照片”,然后将实际图像存储在设备上。
目前我将图像存储在位图中,例如
// create the directory that the app uses to store pending
// uploads
String photoStoragePath = Environment
.getExternalStorageDirectory()
+ Photo.UPLOAD_DIRECTORY;
// create the directory
File storageDirectory = new File(photoStoragePath);
storageDirectory.mkdirs();
// used for filename uid
long millis = System.currentTimeMillis();
// define the file_name to save photo taken by camera
// activity
String fileName = millis + ".jpg";
File file = new File(photoStoragePath, fileName);
// temporarily store the photo
mLastPhoto.compress(CompressFormat.JPEG, 95,
new FileOutputStream(file.getAbsolutePath()));
但是在拍了 5 张左右的照片后,我得到了这个 OOM Exception
09-26 01:55:14.979: E/AndroidRuntime(6303): FATAL EXCEPTION: main
09-26 01:55:14.979: E/AndroidRuntime(6303): java.lang.OutOfMemoryError
09-26 01:55:14.979: E/AndroidRuntime(6303): at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
09-26 01:55:14.979: E/AndroidRuntime(6303): at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:426)
09-26 01:55:14.979: E/AndroidRuntime(6303): at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:444)
09-26 01:55:14.979: E/AndroidRuntime(6303): at com.company.app.ui.CameraActivity$3$1$2.onPictureTaken(CameraActivity.java:203)
09-26 01:55:14.979: E/AndroidRuntime(6303): at android.hardware.Camera$EventHandler.handleMessage(Camera.java:734)
09-26 01:55:14.979: E/AndroidRuntime(6303): at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 01:55:14.979: E/AndroidRuntime(6303): at android.os.Looper.loop(Looper.java:137)
09-26 01:55:14.979: E/AndroidRuntime(6303): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-26 01:55:14.979: E/AndroidRuntime(6303): at java.lang.reflect.Method.invokeNative(Native Method)
是否有必要像这样将图像存储在内存中,或者我是否缺少一些可以使这更容易的东西?