在 Android 相机意图上按后退按钮时应用程序崩溃
我在 Android 应用程序中使用相机时遇到问题
,我有一个用于保存数据的表单,有一个使用相机拍照的按钮,并且从相机捕获的图像将放置在我的表单中的 ImageView 上。
调用相机意图并将图像放置在我的问题是的形式中没有问题
: 在相机应用程序中, 如果我按后退按钮(返回到表单),我的应用程序崩溃了。
为什么?
这是我的按钮上调用相机的代码
btn_takepic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
这是我的 onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final EditText et_foto = (EditText) findViewById(R.id.et_foto);
if(requestCode == CAMERA_PIC_REQUEST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.imv_foto);
image.setImageBitmap(thumbnail);
//toastkeun(data.getExtras().get("data").toString());
}
super.onActivityResult(requestCode, resultCode, data);
}
我正在真实设备上测试 这是我这一行的 logCat
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): FATAL EXCEPTION: main
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {com.ngimagrid.nigmago/com.ngimagrid.nigmago.FormAsetTambah}: java.lang.NullPointerException
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.access$2800(ActivityThread.java:125)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.os.Looper.loop(Looper.java:123)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at java.lang.reflect.Method.invoke(Method.java:521)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at dalvik.system.NativeStart.main(Native Method)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): Caused by: java.lang.NullPointerException
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at com.ngimagrid.nigmago.FormAsetTambah.onActivityResult(FormAsetTambah.java:246)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.Activity.dispatchActivityResult(Activity.java:3890)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): ... 11 more
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): FATAL EXCEPTION: main
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {com.ngimagrid.nigmago/com.ngimagrid.nigmago.FormAsetTambah}: java.lang.NullPointerException
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.access$2800(ActivityThread.java:125)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.os.Looper.loop(Looper.java:123)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at java.lang.reflect.Method.invoke(Method.java:521)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at dalvik.system.NativeStart.main(Native Method)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): Caused by: java.lang.NullPointerException
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at com.ngimagrid.nigmago.FormAsetTambah.onActivityResult(FormAsetTambah.java:246)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.Activity.dispatchActivityResult(Activity.java:3890)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): ... 11 more
,我认为这就是问题所在......
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {com.ngimagrid.nigmago/com.ngimagrid.nigmago.FormAsetTambah}: java.lang.NullPointerException
没有返回结果,对吗? 如何解决这个问题?
谢谢
cmiiw
i have problem using camera in my Android application
i have a form for save data, there is a button to Take Picture with camera and the captured image from the camera will be placed on ImageView in my form.
there's no problem on calling camera intent and placing the image in the form
my problem is :
in the camera application,
if i press back button (back to the form) my application crashed.
why?
this is the code on my button that call the camera
btn_takepic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
this is my onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final EditText et_foto = (EditText) findViewById(R.id.et_foto);
if(requestCode == CAMERA_PIC_REQUEST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.imv_foto);
image.setImageBitmap(thumbnail);
//toastkeun(data.getExtras().get("data").toString());
}
super.onActivityResult(requestCode, resultCode, data);
}
i'm testing on real device
this is my logCat
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): FATAL EXCEPTION: main
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {com.ngimagrid.nigmago/com.ngimagrid.nigmago.FormAsetTambah}: java.lang.NullPointerException
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.access$2800(ActivityThread.java:125)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.os.Looper.loop(Looper.java:123)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at java.lang.reflect.Method.invoke(Method.java:521)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at dalvik.system.NativeStart.main(Native Method)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): Caused by: java.lang.NullPointerException
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at com.ngimagrid.nigmago.FormAsetTambah.onActivityResult(FormAsetTambah.java:246)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.Activity.dispatchActivityResult(Activity.java:3890)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): ... 11 more
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): FATAL EXCEPTION: main
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {com.ngimagrid.nigmago/com.ngimagrid.nigmago.FormAsetTambah}: java.lang.NullPointerException
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.access$2800(ActivityThread.java:125)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.os.Looper.loop(Looper.java:123)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at java.lang.reflect.Method.invoke(Method.java:521)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at dalvik.system.NativeStart.main(Native Method)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): Caused by: java.lang.NullPointerException
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at com.ngimagrid.nigmago.FormAsetTambah.onActivityResult(FormAsetTambah.java:246)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.Activity.dispatchActivityResult(Activity.java:3890)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): ... 11 more
from this line, i think that's the problem...
07-08 16:05:26.187: ERROR/AndroidRuntime(12116): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {com.ngimagrid.nigmago/com.ngimagrid.nigmago.FormAsetTambah}: java.lang.NullPointerException
there's no result returned right?
how to fix that?
thanks
cmiiw
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
NullPointerException 是由您的代码假设 data.getExtras().get("data") 将返回某些内容引起的。这是一个完全未记录的参数,在许多(大多数)设备上将返回 null。
如果你想获取缩略图,你可以尝试这样的方法,它会尝试两种已知的(对我来说,无论如何)方法从相机意图的结果中提取缩略图。
顺便说一句,我认为还有另一个错误 - 您的 onActivityResult 还应该测试 resultCode 是否正常 - 如果用户从相机取消,我相信 onActivityResult 仍然会被调用。
我希望这有用。
The NullPointerException is caused by your code assuming that data.getExtras().get("data") will return something. This is an entirely undocumented parameter and on many (most) devices will return null.
If, as it appears, you're trying to get a thumbnail, you can try something like this, which tries both known (to me, anyway) ways in which to extract a thumbnail from the results of the Camera Intent.
Incidentally I think there's another bug - your onActivityResult should also test that resultCode is OK - if the user cancels from the Camera I believe onActivityResult will still be called.
I hope that's useful.
我认为这不是来自您的应用程序。您可以尝试将整个 onActivity 结果代码包含在 try/catch 块中。我认为您遇到的是某些手机上的相机错误。还尝试尝试根据 startActivityForResult 意图发送的附加内容的不同组合。
在执行任何操作之前,请务必检查结果代码。如果结果被取消(Activity.RESULT_CANCELED)或确定(Activity.RESULT_OK),您不想制作相同的代码,
而且还有一个错误,结果没有出现在 EXTRA_OUTPUT 中(我认为它是这样命名的)但是相反,它作为 Intent.getData() 中的 Uri 出现。额外的输出是您提供保存文件的路径,并且返回的 uri 通常是保存在默认相机位置的图像。正如您将看到的,它有点不同。
您没有使用flags atm,但如果您想要图像位置,则应该使用flags atm。在堆栈中搜索一些额外的示例代码。
I don't think this is coming from your app. you can try to surround the whole onActivity result code in a try/catch block. i think what you are experiencing is a Camera bug on some of the phones. also try to try around different combinations of the extras you are sending on startActivityForResult intent.
Also always check the result code before you do anything. you dont want to make the same code if the result is canceled(Activity.RESULT_CANCELED) or ok (Activity.RESULT_OK )
also there is a bug where the result doesn't come in the EXTRA_OUTPUT (i think it was named like that) but instead it comes as an Uri in intent.getData(). extra output is you providing a path where to save the file and the uri return is usually the image saved in the default camera location. its a bit different as you will see.
You are not using flags atm but you should if you want the image location as a result. Search stack for some sample code for the extras.
是的,您可以通过这种方式在画廊或相机或任何其他设备上处理后退按钮。
Yes you can handle back button while on gallery or camera or any other by this way.
我认为这可以通过一个简单的技巧来解决
I think this can be solved with a simple trick
你谈论的是我知道的 YouTube 视频,我也遇到了同样的错误。
Your talking about the youtube video I know, I had the same error.