我无法从我开发的相机意图接收数据捕获。我使用了 android API 指南中的代码。谁能告诉我哪里出错了?我可以看到 LogCat 打印输出中存在的who=null
和data=null
参数可能会导致错误,但我不确定为什么会这样。
相机活动
//ESSENTIAL VARIABLES - DD - 29/04/2013
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private static final int MEDIA_TYPE_IMAGE = 1;
private Uri fileUri;
MenuItem item;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_snap_camera);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
/**
* Gets the OutputMediaFileUri and accepts media type as a parameter
* @param type
* @return
*/
private static Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
/**
* Method taking media type as a parameter and will save images taken to a public directory on users' device.
* @param type
* @return
*/
private static File getOutputMediaFile(int type){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "CrowdSnapCymru");
if(!mediaStorageDir.exists()){
if(! mediaStorageDir.mkdirs()){
Log.d("CrowdSnapCymru", "failed to create photo directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if(type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
}
else{
return null;
}
return mediaFile;
}
/**
* Receives the result of Camera intent.
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if(resultCode == RESULT_OK){
Toast.makeText(this, "Image saved to: \n" + data.getData() , Toast.LENGTH_LONG).show();
}
else if(resultCode == RESULT_CANCELED){
Toast.makeText(this, "User canceled the image capture", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this, "Image capture failed. Please try again", Toast.LENGTH_LONG).show();
}
}
}
代码加载相机应用程序,如果我取消应用程序但如果我接受图像或生成图像,将产生成功的RESULT_OKAY
结果NullPointerException
。
LogCat 打印输出
05-01 09:57:48.894: E/AndroidRuntime(974): FATAL EXCEPTION: main
05-01 09:57:48.894: E/AndroidRuntime(974): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {rcahmw.prototype.crowdsnapcymru/rcahmw.prototype.crowdsnapcymru.SnapCamera}: java.lang.NullPointerException
05-01 09:57:48.894: E/AndroidRuntime(974): at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
05-01 09:57:48.894: E/AndroidRuntime(974): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
05-01 09:57:48.894: E/AndroidRuntime(974): at android.app.ActivityThread.access$1100(ActivityThread.java:141)
05-01 09:57:48.894: E/AndroidRuntime(974): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
05-01 09:57:48.894: E/AndroidRuntime(974): at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 09:57:48.894: E/AndroidRuntime(974): at android.os.Looper.loop(Looper.java:137)
05-01 09:57:48.894: E/AndroidRuntime(974): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-01 09:57:48.894: E/AndroidRuntime(974): at java.lang.reflect.Method.invokeNative(Native Method)
05-01 09:57:48.894: E/AndroidRuntime(974): at java.lang.reflect.Method.invoke(Method.java:511)
05-01 09:57:48.894: E/AndroidRuntime(974): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-01 09:57:48.894: E/AndroidRuntime(974): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-01 09:57:48.894: E/AndroidRuntime(974): at dalvik.system.NativeStart.main(Native Method)
05-01 09:57:48.894: E/AndroidRuntime(974): Caused by: java.lang.NullPointerException
05-01 09:57:48.894: E/AndroidRuntime(974): at rcahmw.prototype.crowdsnapcymru.SnapCamera.onActivityResult(SnapCamera.java:84)
05-01 09:57:48.894: E/AndroidRuntime(974): at android.app.Activity.dispatchActivityResult(Activity.java:5293)
05-01 09:57:48.894: E/AndroidRuntime(974): at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
任何建议都会很棒。在 android 中使用相机应用程序非常新。