发送失败结果ResultInfo android

时间:2014-02-06 17:50:34

标签: android android-camera runtime-error

我的应用程序是当用户点击拍照片按钮时,它会将图片保存在外部存储器中。当我在设备中检查我的文件夹时它正在工作。但我不明白为什么我的意图等于null我将theExtra置于intent.And

这是我的代码

private void captureImage(){
    //check Is device support a camera??
    isDeviceSupportCamera();
    if(isDeviceSupportCamera() == true){
        //intent to camera process
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //สร้างไฟล์ใหม่มาลองรับ รูปที่ถูกถ่าย ไปยัง picDirectory
        imageFile = new File(Environment.getExternalStorageDirectory()+path,
                    "img_"+System.currentTimeMillis()+".jpg");

        startActivityForResult(intent, imageCode);
    }else{
        System.out.println("Your device is not supprot feature camera");
    }

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(requestCode == imageCode && resultCode == RESULT_OK){



            //add imageFile to array File
            fileCapture.add(imageFile);
            //set adapter
            gridView.setAdapter(new gridviewAdapter(context, fileCapture));


    }else if(requestCode == imageCode && resultCode == RESULT_CANCELED){
        Toast.makeText(getApplicationContext(), "user cancle a image capture", Toast.LENGTH_LONG).show();
    }else{
        Toast.makeText(getApplicationContext(), "Failed to capture", Toast.LENGTH_LONG).show();
    }

}

这是logcat错误

02-07 00:48:05.666: E/AndroidRuntime(18495): FATAL EXCEPTION: main
02-07 09:37:34.625: E/AndroidRuntime(24772): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/14298 (has extras) }} to activity {com.example.newlookrecipe/com.example.newlookrecipe.MainActivity}: java.lang.NullPointerException{com.example.newlookrecipe/com.example.newlookrecipe.MainActivity}: java.lang.NullPointerException
02-07 00:48:05.666: E/AndroidRuntime(18495):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3205)
02-07 00:48:05.666: E/AndroidRuntime(18495):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3248)
02-07 00:48:05.666: E/AndroidRuntime(18495):    at android.app.ActivityThread.access$1200(ActivityThread.java:140)
02-07 00:48:05.666: E/AndroidRuntime(18495):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
02-07 00:48:05.666: E/AndroidRuntime(18495):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 00:48:05.666: E/AndroidRuntime(18495):    at android.os.Looper.loop(Looper.java:137)
02-07 00:48:05.666: E/AndroidRuntime(18495):    at android.app.ActivityThread.main(ActivityThread.java:4921)
02-07 00:48:05.666: E/AndroidRuntime(18495):    at java.lang.reflect.Method.invokeNative(Native Method)
02-07 00:48:05.666: E/AndroidRuntime(18495):    at java.lang.reflect.Method.invoke(Method.java:511)
02-07 00:48:05.666: E/AndroidRuntime(18495):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
02-07 00:48:05.666: E/AndroidRuntime(18495):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
02-07 00:48:05.666: E/AndroidRuntime(18495):    at dalvik.system.NativeStart.main(Native Method)
02-07 00:48:05.666: E/AndroidRuntime(18495): Caused by: java.lang.NullPointerException
02-07 00:48:05.666: E/AndroidRuntime(18495):    at com.example.newlookrecipe.MainActivity.onActivityResult(MainActivity.java:96)
02-07 00:48:05.666: E/AndroidRuntime(18495):    at android.app.Activity.dispatchActivityResult(Activity.java:5390)
02-07 00:48:05.666: E/AndroidRuntime(18495):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3201)
02-07 00:48:05.666: E/AndroidRuntime(18495):    ... 11 more

1 个答案:

答案 0 :(得分:3)

来自消息来源:

/**
 * Standard Intent action that can be sent to have the camera application
 * capture an image and return it.
 * <p>
 * The caller may pass an extra EXTRA_OUTPUT to control where this image will be written.
 * If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap
 * object in the extra field. This is useful for applications that only need a small image.
 * If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri
 * value of EXTRA_OUTPUT.
 * @see #EXTRA_OUTPUT
 */
@SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
public final static String ACTION_IMAGE_CAPTURE = "android.media.action.IMAGE_CAPTURE";

注意如果EXTRA_OUTPUT不存在

由于您包含intent.putExtra(MediaStore.EXTRA_OUTPUT, uri.fromFile(imageFile));,因此您表达的是Intent(MediaStore.ACTION_IMAGE_CAPTURE),目的是将捕获的图像保存到文件系统。 Android知道您知道图像的位置,因此如果您在未指定此额外内容的情况下启动Intent,它将不会回复它将提供的缩略图图像。由于它不发送数据,因此Intent对象将为空。

如果您想要缩略图,或将图片添加到图片视图,只需使用您提供给Uri的{​​{1}}。