【问题标题】:Failure delivering result ResultInfo android传递结果ResultInfo android失败
【发布时间】:2014-02-06 17:50:33
【问题描述】:

我的应用程序是当用户单击拍照按钮时,它会将图片保存在外部存储中。在这张脸中,当我在设备中签入我的文件夹时它正在工作。但我不明白为什么我的意图等于 null ,而我把Extra放入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

【问题讨论】:

    标签: android android-camera runtime-error


    【解决方案1】:

    来源:

    /**
     * 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 对象将为空。

    如果您想要缩略图图像,或者要将图像添加到图像视图,您只需使用您提供给IntentUri

    【讨论】:

    • 谢谢 Submersed。如果我想要一个缩略图捕获,我可以从 imageFile.getAbsolutePath();.现在我从代码中删除这一行。"intent.putExtra(MediaStore.EXTRA_OUTPUT, uri. fromFile(imageFile));",但还是报错,请您再检查一下。
    • 因此,从您提供的代码来看,您甚至没有访问 onActivityResult 中的 Intent 对象。您确定 Intent 对象为 null 是问题所在,还是您的“fileCapture”或“gridView”?
    • 另外,遵循培训文档可能会有所帮助:developer.android.com/training/camera/photobasics.htmla
    • 谢谢,我尝试了很长时间的代码。现在不是错误
    猜你喜欢
    • 2012-04-07
    • 2014-01-13
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    相关资源
    最近更新 更多