在 Android 中使用 takePicture 时未调用 PictureCallback
我正在使用自己的相机拍照并尝试将结果保存到文件系统(/data/data/package.name/file/captured_photo1.jpeg)。当我在模拟器(Android 2.2)上尝试它时,它似乎可以工作 - 创建一个大小为 8733 字节的文件并显示它(模拟器的默认照片)。问题是,当我在 GalaxyS (Android 2.2.1) 上尝试时,文件系统上的图像为 0 字节,
我发现(从日志中)我传递给 takePicture 方法的回调之一未被调用。真正拯救它的是它。
我在 rawCallback 中获得的 data[] 在手机和模拟器上都是 null,但在模拟器上调用了回调并且它可以工作,
这是整个 Activity 代码:
public class SolveCaptureActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.solve_capture);
preview = new Preview(this);
((FrameLayout) findViewById(R.id.preview)).addView(preview);
buttonClick = (Button) findViewById(R.id.buttonCapture);
buttonClick.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
Intent gotoImagePreview = new Intent(v.getContext(), CapturedImagePreview.class);
startActivityForResult(gotoImagePreview, 0);
}
});
buttonFocus = (Button) findViewById(R.id.buttonFocus);
buttonFocus.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
preview.camera.autoFocus(new AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
Camera.Parameters camParam = camera.getParameters();
camParam.setFocusMode(Parameters.FOCUS_MODE_AUTO);
camera.setParameters(camParam);
}
});
}
});
Log.d(TAG, "onCreate'd");
}
// Called when shutter is opened
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
Log.d(TAG, "onShutter'd");
}
};
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.d(TAG, "onPictureTaken - raw");
}
};
PictureCallback jpegCallback = new PictureCallback() { // <8>
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
if (data == null){
Log.d("@@--DATA--@@, msg","=NULL");
}
try {
Log.d("@@--DIR--@@", Environment.getExternalStorageDirectory().getAbsolutePath());
Log.d("@@--Size--@@", Integer.toString(data.length));
outStream = getApplicationContext().openFileOutput(SolveCaptureActivity.FILE_NAME, Context.MODE_WORLD_READABLE);
outStream.write(data);
outStream.flush();
outStream.close();
Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) { // <10>
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Log.d(TAG, "onPictureTaken - jpeg");
}
};
}
这是日志:
09-01 18:44:38.403: WARN/CameraService(6488): takePicture (pid 6585)
09-01 18:44:38.403: INFO/ShotSingle(6488): ShotSingle::takePicture start
09-01 18:44:38.403: ERROR/CameraHardwareSec(6488): stopPreview()
09-01 18:44:38.403: ERROR/SecCamera(6488): cancelAutofocus()
09-01 18:44:38.403: ERROR/SecCamera(6488): cancelAutofocus() end, 0, 4
09-01 18:44:38.409: ERROR/SecCamera(6488): stopPreview()
09-01 18:44:38.409: ERROR/SecCamera(6488): fimc_v4l2_streamoff()
09-01 18:44:38.433: ERROR/CameraHardwareSec(6488): stopPreview() end
09-01 18:44:38.433: INFO/ShotSingle(6488): ShotSingle::takePicture end
09-01 18:44:38.433: INFO/ActivityManager(3135): Starting activity: Intent { cmp=cs.workshop.solvedroid/.CapturedImagePreview }
09-01 18:44:38.437: WARN/CameraService(6488): setPreviewCallbackFlag (pid 6585)
09-01 18:44:38.437: DEBUG/Preview(6585): onPreviewFrame called at: 1314891878438
09-01 18:44:38.457: DEBUG/SecCamera(6488): passed fmt = 1498831189 found pixel format[3]: YUV 4:2:2 packed, CbYCrY
09-01 18:44:38.467: WARN/CameraService(6488): width(640), height(480), format:jpeg
09-01 18:44:38.523: WARN/System.err(6585): java.io.FileNotFoundException: /data/data/cs.workshop.solvedroid/files/captured_photo2.jpeg (No such file or directory)
09-01 18:44:38.542: WARN/System.err(6585): at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
09-01 18:44:38.542: WARN/System.err(6585): at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
09-01 18:44:38.542: WARN/System.err(6585): at java.io.FileInputStream.<init>(FileInputStream.java:82)
09-01 18:44:38.542: WARN/System.err(6585): at android.app.ContextImpl.openFileInput(ContextImpl.java:448)
09-01 18:44:38.542: WARN/System.err(6585): at android.content.ContextWrapper.openFileInput(ContextWrapper.java:152)
09-01 18:44:38.542: WARN/System.err(6585): at cs.workshop.solvedroid.CapturedImagePreview.onCreate(CapturedImagePreview.java:29)
09-01 18:44:38.542: WARN/System.err(6585): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-01 18:44:38.543: WARN/System.err(6585): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-01 18:44:38.543: WARN/System.err(6585): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-01 18:44:38.545: WARN/System.err(6585): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-01 18:44:38.545: WARN/System.err(6585): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-01 18:44:38.547: WARN/System.err(6585): at android.os.Handler.dispatchMessage(Handler.java:99)
09-01 18:44:38.547: WARN/System.err(6585): at android.os.Looper.loop(Looper.java:123)
09-01 18:44:38.549: WARN/System.err(6585): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-01 18:44:38.549: WARN/System.err(6585): at java.lang.reflect.Method.invokeNative(Native Method)
09-01 18:44:38.551: WARN/System.err(6585): at java.lang.reflect.Method.invoke(Method.java:521)
09-01 18:44:38.553: WARN/System.err(6585): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
09-01 18:44:38.553: WARN/System.err(6585): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-01 18:44:38.555: WARN/System.err(6585): at dalvik.system.NativeStart.main(Native Method)
09-01 18:44:38.643: WARN/CameraService(6488): stopPreview (pid 6585)
09-01 18:44:38.643: ERROR/CameraHardwareSec(6488): stopPreview()
09-01 18:44:38.643: ERROR/SecCamera(6488): cancelAutofocus()
09-01 18:44:38.643: ERROR/SecCamera(6488): cancelAutofocus() end, 0, 4
09-01 18:44:38.643: ERROR/SecCamera(6488): stopPreview()
09-01 18:44:38.645: ERROR/SecCamera(6488): stopPreview: m_flag_camera_start is zero
09-01 18:44:38.645: ERROR/CameraHardwareSec(6488): stopPreview() end
09-01 18:44:38.645: WARN/CameraService(6488): stopPreview(), hardware stopped OK
I'm taking a picture using my own camera and trying to save the result to the file system (/data/data/package.name/file/captured_photo1.jpeg). When i try it on the emulator (Android 2.2) it seems to work - creates a file with size 8733 bytes and displays it (emulator's default photo). The problem is that when i try it on my GalaxyS (Android 2.2.1) the image on the file system is 0 bytes
i found out (from the log) that one of the callbacks i pass to my takePicture method isn't called. and it's the one that actually saves it.
the data[] that i get in the rawCallback is null both on the phone and the emulator, but on the emulator the callback is called and it works
this is the whole Activity code:
public class SolveCaptureActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.solve_capture);
preview = new Preview(this);
((FrameLayout) findViewById(R.id.preview)).addView(preview);
buttonClick = (Button) findViewById(R.id.buttonCapture);
buttonClick.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
Intent gotoImagePreview = new Intent(v.getContext(), CapturedImagePreview.class);
startActivityForResult(gotoImagePreview, 0);
}
});
buttonFocus = (Button) findViewById(R.id.buttonFocus);
buttonFocus.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
preview.camera.autoFocus(new AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
Camera.Parameters camParam = camera.getParameters();
camParam.setFocusMode(Parameters.FOCUS_MODE_AUTO);
camera.setParameters(camParam);
}
});
}
});
Log.d(TAG, "onCreate'd");
}
// Called when shutter is opened
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
Log.d(TAG, "onShutter'd");
}
};
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.d(TAG, "onPictureTaken - raw");
}
};
PictureCallback jpegCallback = new PictureCallback() { // <8>
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
if (data == null){
Log.d("@@--DATA--@@, msg","=NULL");
}
try {
Log.d("@@--DIR--@@", Environment.getExternalStorageDirectory().getAbsolutePath());
Log.d("@@--Size--@@", Integer.toString(data.length));
outStream = getApplicationContext().openFileOutput(SolveCaptureActivity.FILE_NAME, Context.MODE_WORLD_READABLE);
outStream.write(data);
outStream.flush();
outStream.close();
Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) { // <10>
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Log.d(TAG, "onPictureTaken - jpeg");
}
};
}
this is the log:
09-01 18:44:38.403: WARN/CameraService(6488): takePicture (pid 6585)
09-01 18:44:38.403: INFO/ShotSingle(6488): ShotSingle::takePicture start
09-01 18:44:38.403: ERROR/CameraHardwareSec(6488): stopPreview()
09-01 18:44:38.403: ERROR/SecCamera(6488): cancelAutofocus()
09-01 18:44:38.403: ERROR/SecCamera(6488): cancelAutofocus() end, 0, 4
09-01 18:44:38.409: ERROR/SecCamera(6488): stopPreview()
09-01 18:44:38.409: ERROR/SecCamera(6488): fimc_v4l2_streamoff()
09-01 18:44:38.433: ERROR/CameraHardwareSec(6488): stopPreview() end
09-01 18:44:38.433: INFO/ShotSingle(6488): ShotSingle::takePicture end
09-01 18:44:38.433: INFO/ActivityManager(3135): Starting activity: Intent { cmp=cs.workshop.solvedroid/.CapturedImagePreview }
09-01 18:44:38.437: WARN/CameraService(6488): setPreviewCallbackFlag (pid 6585)
09-01 18:44:38.437: DEBUG/Preview(6585): onPreviewFrame called at: 1314891878438
09-01 18:44:38.457: DEBUG/SecCamera(6488): passed fmt = 1498831189 found pixel format[3]: YUV 4:2:2 packed, CbYCrY
09-01 18:44:38.467: WARN/CameraService(6488): width(640), height(480), format:jpeg
09-01 18:44:38.523: WARN/System.err(6585): java.io.FileNotFoundException: /data/data/cs.workshop.solvedroid/files/captured_photo2.jpeg (No such file or directory)
09-01 18:44:38.542: WARN/System.err(6585): at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
09-01 18:44:38.542: WARN/System.err(6585): at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
09-01 18:44:38.542: WARN/System.err(6585): at java.io.FileInputStream.<init>(FileInputStream.java:82)
09-01 18:44:38.542: WARN/System.err(6585): at android.app.ContextImpl.openFileInput(ContextImpl.java:448)
09-01 18:44:38.542: WARN/System.err(6585): at android.content.ContextWrapper.openFileInput(ContextWrapper.java:152)
09-01 18:44:38.542: WARN/System.err(6585): at cs.workshop.solvedroid.CapturedImagePreview.onCreate(CapturedImagePreview.java:29)
09-01 18:44:38.542: WARN/System.err(6585): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-01 18:44:38.543: WARN/System.err(6585): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-01 18:44:38.543: WARN/System.err(6585): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-01 18:44:38.545: WARN/System.err(6585): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-01 18:44:38.545: WARN/System.err(6585): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-01 18:44:38.547: WARN/System.err(6585): at android.os.Handler.dispatchMessage(Handler.java:99)
09-01 18:44:38.547: WARN/System.err(6585): at android.os.Looper.loop(Looper.java:123)
09-01 18:44:38.549: WARN/System.err(6585): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-01 18:44:38.549: WARN/System.err(6585): at java.lang.reflect.Method.invokeNative(Native Method)
09-01 18:44:38.551: WARN/System.err(6585): at java.lang.reflect.Method.invoke(Method.java:521)
09-01 18:44:38.553: WARN/System.err(6585): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
09-01 18:44:38.553: WARN/System.err(6585): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-01 18:44:38.555: WARN/System.err(6585): at dalvik.system.NativeStart.main(Native Method)
09-01 18:44:38.643: WARN/CameraService(6488): stopPreview (pid 6585)
09-01 18:44:38.643: ERROR/CameraHardwareSec(6488): stopPreview()
09-01 18:44:38.643: ERROR/SecCamera(6488): cancelAutofocus()
09-01 18:44:38.643: ERROR/SecCamera(6488): cancelAutofocus() end, 0, 4
09-01 18:44:38.643: ERROR/SecCamera(6488): stopPreview()
09-01 18:44:38.645: ERROR/SecCamera(6488): stopPreview: m_flag_camera_start is zero
09-01 18:44:38.645: ERROR/CameraHardwareSec(6488): stopPreview() end
09-01 18:44:38.645: WARN/CameraService(6488): stopPreview(), hardware stopped OK
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该等待
jpegCallback.onPictureTaken()
被调用,然后才启动另一个活动:You should wait
jpegCallback.onPictureTaken()
to be called and only then launch another activity: