gpt4 book ai didi

java - 从相机捕获图像后传递额外的 Intent

转载 作者:行者123 更新时间:2023-11-30 02:26:30 25 4
gpt4 key购买 nike

我想在从相机捕获图像后传递一个额外的 Intent 来识别我捕获的图像。为此,我有,

int TAKE_PHOTO_CODE = 0;
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);

响应时,

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PHOTO_CODE && resultCode == EditProfileActivity.RESULT_OK) {
Log.d("CameraDemo", "Pic saved");
// Get passed intent here...
System.out.println("EXTRA INTENT: "+data.getStringExtra("profile_pic"));
}
}

上面的代码工作正常。我正在本地存储中获取图像。但是,我想再传递一个字符串 Intent 。

有点像,

cameraIntent.putExtra("profile_pic", "true");

在响应方面,我想得到那个 Intent 。但是,我遇到了 nullpointer exception。传递 Intent 的正确方法是什么?

堆栈跟踪

01-09 13:29:36.228  23214-23214/com.example.android.navigationdrawerexample E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.android.navigationdrawerexample, PID: 23214
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.example.android.navigationdrawerexample/com.example.android.mtesapp.EditProfileActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3942)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3992)
at android.app.ActivityThread.access$1300(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.android.mtesapp.EditProfileActivity.onActivityResult(EditProfileActivity.java:316)
at android.app.Activity.dispatchActivityResult(Activity.java:5535)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3938)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3992)
at android.app.ActivityThread.access$1300(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

onActivityResult 中返回的 Intent 不一定与您发送给其他 Activity 的结果相同 - 它是一个包含 Activity 结果的新 Intent。所以 profile_pic 不在那里,因为你调用的 Activity 没有把它放在那里。

如果你想像这样保存数据,你需要在你的应用程序中的某个地方的变量中进行。

关于java - 从相机捕获图像后传递额外的 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27866785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com