onActivityResult从相机回来,意图空意图、相机、onActivityResult

2023-09-05 23:32:32 作者:与昨日相同

我遵循 Android开发网站

我刚刚启动摄像头意图,而不是建立自己的相机。

样品code到拍摄照片如下所示后,办理结果的回报。

 保护无效onActivityResult(INT申请code,INT结果code,意图数据){
    如果(要求code == CAPTURE_IMAGE_ACTIVITY_REQUEST_ code){
        如果(结果code == RESULT_OK){
            //图像捕获并保存到了fileURI在Intent指定
            Toast.makeText(这一点,图片保存到:\ N+
                    data.getData(),Toast.LENGTH_LONG).show();
        }否则,如果(结果code == RESULT_CANCELED){
            //用户取消了图像采集
        } 其他 {
            //图像捕捉失​​败,提醒用户
        }
    }

    如果(要求code == CAPTURE_VIDEO_ACTIVITY_REQUEST_ code){
        如果(结果code == RESULT_OK){
            //视频捕获并保存到了fileURI在Intent指定
            Toast.makeText(这一点,视频保存到:\ N+
                    data.getData(),Toast.LENGTH_LONG).show();
        }否则,如果(结果code == RESULT_CANCELED){
            //用户取消了视频拍摄
        } 其他 {
            //视频捕获失败,提醒用户
        }
    }
}
 

结果code 是美好的,但数据始终为NULL,这会导致NPE。我看着的SD卡,照片是真的救了那里。任何提示? TKS了。

更新:logcat的信息的要求:

  01-28 19:39:00.547:ERROR / AndroidRuntime(24315):致命异常:主要
    java.lang.RuntimeException的:无法恢复活动{com.example.CameraTest / com.example.CameraTest.MyCamera}:java.lang.RuntimeException的:不提供结果ResultInfo {谁= NULL,请求= 100,结果= -1,数据= NULL}到活动{com.example.CameraTest / com.example.CameraTest.MyCamera}:显示java.lang.NullPointerException
    在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2455)
    在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2483)
    在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1997)
    在android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3362)
    在android.app.ActivityThread.access $ 700(ActivityThread.java:127)
    在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1162)
    在android.os.Handler.dispatchMessage(Handler.java:99)
    在android.os.Looper.loop(Looper.java:137)
    在android.app.ActivityThread.main(ActivityThread.java:4511)
    在java.lang.reflect.Method.invokeNative(本机方法)
    在java.lang.reflect.Method.invoke(Method.java:511)
    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:980)
    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
    在dalvik.system.NativeStart.main(本机方法)
    致:java.lang.RuntimeException的:不提供结果ResultInfo {谁= NULL,请求= 100,结果= -1,数据= NULL}到活动{com.example.CameraTest / com.example.CameraTest.MyCamera}:JAVA .lang.NullPointerException
    在android.app.ActivityThread.deliverResults(ActivityThread.java:2991)
    在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2442)
    ... 13更多
    显示java.lang.NullPointerException:产生的原因
    在com.example.CameraTest.MyCamera.onActivityResult(MyCamera.java:71)
    在android.app.Activity.dispatchActivityResult(Activity.java:4654)
    在android.app.ActivityThread.deliverResults(ActivityThread.java:2987)
    ... 14更多
 

解决方案

您code问题是这样的:

  data.getData()
 
EOS 200D 正式发布,小身材大世界

该调用不会获得额外用钥匙从返回内容意图。它得到了场数据从返回意图这也许是

您需要获得额外的从返回意图是这样的:

  data.getExtras()获得(数据)。
 

一些其他的答案都表明这一点,嵌入吨,其他code。这使得它很难真正看到的问题是什么。

I follow the instruction on Camera on Android dev site

I just start the Camera Intent, not build my own camera.

The sample code to handle result return after taking a photo is as follows.

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent
            Toast.makeText(this, "Image saved to:\n" +
                    data.getData(), Toast.LENGTH_LONG).show();
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
        }
    }

    if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Video captured and saved to fileUri specified in the Intent
            Toast.makeText(this, "Video saved to:\n" +
                    data.getData(), Toast.LENGTH_LONG).show();
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the video capture
        } else {
            // Video capture failed, advise user
        }
    }
}

resultCode is OK, but data is always NULL, which causes a NPE. I looked into the sdcard, the photo was really saved there. Any tip? tks much.

Update: logcat info as requested:

   01-28 19:39:00.547: ERROR/AndroidRuntime(24315): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to resume activity {com.example.CameraTest/com.example.CameraTest.MyCamera}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.example.CameraTest/com.example.CameraTest.MyCamera}: java.lang.NullPointerException
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2455)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2483)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1997)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3362)
    at android.app.ActivityThread.access$700(ActivityThread.java:127)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1162)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4511)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.example.CameraTest/com.example.CameraTest.MyCamera}: java.lang.NullPointerException
    at android.app.ActivityThread.deliverResults(ActivityThread.java:2991)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2442)
    ... 13 more
    Caused by: java.lang.NullPointerException
    at com.example.CameraTest.MyCamera.onActivityResult(MyCamera.java:71)
    at android.app.Activity.dispatchActivityResult(Activity.java:4654)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:2987)
    ... 14 more                                                                                      

解决方案

The problem with your code is this:

data.getData()

This call does not get the extra with the key "data" from the returned Intent. It gets the field data from the returned Intent which is probably null.

You need to get the extra from the returned Intent like this:

data.getExtras().get("data");

Some of the other answers have indicated this, embedded in tons of other code. That makes it difficult to actually see what the problem is.