我正在使用此代码调用相机意图......
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
Log.d("========CALL CAPTURE========= > URL ", "" + fileUri);
Log.d("========CALL CAPTURE========= > LOGGED TEXT",
"===============>>> LOGGED TEXT");
// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
此处的onActivityResult方法接收调用
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("----------->> Request Code", "" + requestCode);
Log.d("----------->> Request Code", "" + resultCode);
// if the result is capturing Image
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Log.d("========CAPTURE========= > LOGGED TEXT",
"===============>>> LOGGED TEXT");
// successfully captured the image
// display it in image view
previewCapturedImage();
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
} else if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// video successfully recorded
// preview the recorded video
// previewVideo();
} else if (resultCode == RESULT_CANCELED) {
// user cancelled recording
Toast.makeText(getApplicationContext(),
"User cancelled video recording", Toast.LENGTH_SHORT)
.show();
} else {
// failed to record video
Toast.makeText(getApplicationContext(),
"Sorry! Failed to record video", Toast.LENGTH_SHORT)
.show();
}
}
}
但onActivityResult中的日志不打印并给我错误...
Logcat是:
08-25 19:43:33.459: E/AndroidRuntime(7034): FATAL EXCEPTION: main
08-25 19:43:33.459: E/AndroidRuntime(7034): java.lang.NullPointerException
08-25 19:43:33.459: E/AndroidRuntime(7034): at android.app.Activity.startActivityForResult(Activity.java:3195)
08-25 19:43:33.459: E/AndroidRuntime(7034): at com.example.camerahandler.CameraHandler.captureImage(CameraHandler.java:72)
08-25 19:43:33.459: E/AndroidRuntime(7034): at com.example.camerahandler.CameraHandler.startCapture(CameraHandler.java:49)
08-25 19:43:33.459: E/AndroidRuntime(7034): at com.example.camerahandler.MainActivity$1.onClick(MainActivity.java:31)
08-25 19:43:33.459: E/AndroidRuntime(7034): at android.view.View.performClick(View.java:3571)
08-25 19:43:33.459: E/AndroidRuntime(7034): at android.view.View$PerformClick.run(View.java:14247)
08-25 19:43:33.459: E/AndroidRuntime(7034): at android.os.Handler.handleCallback(Handler.java:605)
08-25 19:43:33.459: E/AndroidRuntime(7034): at android.os.Handler.dispatchMessage(Handler.java:92)
08-25 19:43:33.459: E/AndroidRuntime(7034): at android.os.Looper.loop(Looper.java:137)
08-25 19:43:33.459: E/AndroidRuntime(7034): at android.app.ActivityThread.main(ActivityThread.java:4517)
08-25 19:43:33.459: E/AndroidRuntime(7034): at java.lang.reflect.Method.invokeNative(Native Method)
08-25 19:43:33.459: E/AndroidRuntime(7034): at java.lang.reflect.Method.invoke(Method.java:511)
08-25 19:43:33.459: E/AndroidRuntime(7034): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
08-25 19:43:33.459: E/AndroidRuntime(7034): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
08-25 19:43:33.459: E/AndroidRuntime(7034): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
我得到的解决方案为Intent
来电和onActivityResult
不在主视图类中,因此遇到了麻烦。