我正在使用以下代码打开捕获和映像的意图,并使用特定的request_code返回结果
Intent imageCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File mediaStorageDir=new File(Environment.getExternalStorageDirectory(),"zzz");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("zzz", "failed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
Uri imageFile=Uri.fromFile(new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg"));
Log.d("zzz",""+imageFile); // to check whether filename is correct or not!!!
imageCapture.putExtra(MediaStore.EXTRA_OUTPUT, imageFile);
startActivityForResult(imageCapture, 1);
此意图显示了摄像机捕获图像的意图并完成所有操作。它还捕获图像,但是当单击保存按钮时,它会遇到runtimeException。
03-17 09:34:32.324: E/AndroidRuntime(26771): FATAL EXCEPTION: main
03-17 09:34:32.324: E/AndroidRuntime(26771): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {idiot.houses.numberslider/idiot.houses.numberslider.MainActivity}: java.lang.NullPointerException
03-17 09:34:32.324: E/AndroidRuntime(26771): at android.app.ActivityThread.deliverResults(ActivityThread.java:3205)
03-17 09:34:32.324: E/AndroidRuntime(26771): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3248)
03-17 09:34:32.324: E/AndroidRuntime(26771): at android.app.ActivityThread.access$1200(ActivityThread.java:140)
03-17 09:34:32.324: E/AndroidRuntime(26771): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
03-17 09:34:32.324: E/AndroidRuntime(26771): at android.os.Handler.dispatchMessage(Handler.java:99)
03-17 09:34:32.324: E/AndroidRuntime(26771): at android.os.Looper.loop(Looper.java:137)
03-17 09:34:32.324: E/AndroidRuntime(26771): at android.app.ActivityThread.main(ActivityThread.java:4921)
03-17 09:34:32.324: E/AndroidRuntime(26771): at java.lang.reflect.Method.invokeNative(Native Method)
03-17 09:34:32.324: E/AndroidRuntime(26771): at java.lang.reflect.Method.invoke(Method.java:511)
03-17 09:34:32.324: E/AndroidRuntime(26771): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
03-17 09:34:32.324: E/AndroidRuntime(26771): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
03-17 09:34:32.324: E/AndroidRuntime(26771): at dalvik.system.NativeStart.main(Native Method)
03-17 09:34:32.324: E/AndroidRuntime(26771): Caused by: java.lang.NullPointerException
03-17 09:34:32.324: E/AndroidRuntime(26771): at edu.vuum.mocca.ui.story.CreateStoryFragment.onActivityResult(CreateStoryFragment.java:316)
03-17 09:34:32.324: E/AndroidRuntime(26771): at edu.vuum.mocca.ui.story.StoryActivityBase.onActivityResult(StoryActivityBase.java:291)
03-17 09:34:32.324: E/AndroidRuntime(26771): at android.app.Activity.dispatchActivityResult(Activity.java:5372)
03-17 09:34:32.324: E/AndroidRuntime(26771): at android.app.ActivityThread.deliverResults(ActivityThread.java:3201)
03-17 09:34:32.324: E/AndroidRuntime(26771): ... 11 more
答案 0 :(得分:0)
我认为以下代码对您有所帮助。
//code for calling intent
String fileName = "temp.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(intent, PICK_FROM_CAMERA);
//代码写入onActivityResult()方法
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK)
return;
switch (requestCode) {
case PICK_FROM_CAMERA:
String[] projection = { MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null);
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String capturedImageFilePath = cursor.getString(column_index_data);
break;
}
}