我得到一个NullPointerException异常,当我使用ACTION_IMAGE_CAPTURE拍照 [英] I'm getting a NullPointerException when I use ACTION_IMAGE_CAPTURE to take a picture
问题描述
我有一个非常简单的应用程序,从菜单启动相机。相机启动正常,但是当我拍照后点击OK我收到了NPE在我的Nexus One:
E / AndroidRuntime(3891):java.lang.RuntimeException的:不提供结果ResultInfo {谁= NULL,请求= 0,结果= -1,数据= NULL}到活动{ net.asplode.tr/net.asplode.tr.PostImage}:显示java.lang.NullPointerException
E / AndroidRuntime(3891):在android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
E / AndroidRuntime(3891):在android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
E / AndroidRuntime(3891):在android.app.ActivityThread.access $ 2800(ActivityThread.java:125)
E / AndroidRuntime(3891):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2063)
E / AndroidRuntime(3891):在android.os.Handler.dispatchMessage(Handler.java:99)
E / AndroidRuntime(3891):在android.os.Looper.loop(Looper.java:123)
E / AndroidRuntime(3891):在android.app.ActivityThread.main(ActivityThread.java:4627)
E / AndroidRuntime(3891):在java.lang.reflect.Method.invokeNative(本机方法)
E / AndroidRuntime(3891):在java.lang.reflect.Method.invoke(Method.java:521)
E / AndroidRuntime(3891):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
E / AndroidRuntime(3891):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E / AndroidRuntime(3891):在dalvik.system.NativeStart.main(本机方法)
E / AndroidRuntime(3891):显示java.lang.NullPointerException:产生的原因
E / AndroidRuntime(3891):在net.asplode.tr.PostImage.onActivityResult(PostImage.java:92)
E / AndroidRuntime(3891):在android.app.Activity.dispatchActivityResult(Activity.java:3890)
E / AndroidRuntime(3891):在android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
E / AndroidRuntime(3891):11 ...更多
W / ActivityManager(85):强制完成活动net.asplode.tr/.PostImage
code:
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
如果(item.getItemId()== R.id.mnuCamera){
意图cameraIntent =新的意图(MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues值=新ContentValues();
values.put(Media.TITLE,图像);
乌里tempPhotoUri = getContentResolver()插入(Media.EXTERNAL_CONTENT_URI,值)。
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,tempPhotoUri);
startActivityForResult(cameraIntent,FROM_CAMERA);
返回true;
}
@覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
如果(结果code!= RESULT_OK){
返回;
}
乌里imageUri = data.getData();
Log.i(imageUri:,imageUri.toString());
}
原来的股票相机应用程序不发送EXTRA_OUTPUT,这就是为什么它是空。然而,一些摄像头的应用程序(如英雄)做的。真棒。 因此,答案是指定EXTRA_OUTPUT。 Nexus One的相机应用将图片保存到该位置。然后,在onActivityResult()检查是否意图为空。如果不是,则使用data.getData(),并且如果它是再使用特定于EXTRA_OUTPUT位置通过一个常数,将其插入Mediastore。 Urgh。
I have a fairly simple app that launches the camera from a menu. The camera launches fine, but when I hit ok after taking a picture I get a NPE on my nexus one:
E/AndroidRuntime( 3891): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {net.asplode.tr/net.asplode.tr.PostImage}: java.lang.NullPointerException
E/AndroidRuntime( 3891): at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
E/AndroidRuntime( 3891): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
E/AndroidRuntime( 3891): at android.app.ActivityThread.access$2800(ActivityThread.java:125)
E/AndroidRuntime( 3891): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
E/AndroidRuntime( 3891): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 3891): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 3891): at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime( 3891): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 3891): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 3891): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime( 3891): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime( 3891): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 3891): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 3891): at net.asplode.tr.PostImage.onActivityResult(PostImage.java:92)
E/AndroidRuntime( 3891): at android.app.Activity.dispatchActivityResult(Activity.java:3890)
E/AndroidRuntime( 3891): at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
E/AndroidRuntime( 3891): ... 11 more
W/ActivityManager( 85): Force finishing activity net.asplode.tr/.PostImage
Code:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.mnuCamera) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues();
values.put(Media.TITLE, "image");
Uri tempPhotoUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempPhotoUri);
startActivityForResult(cameraIntent, FROM_CAMERA);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
Uri imageUri = data.getData();
Log.i("imageUri: ", imageUri.toString());
}
Turns out the stock camera application doesn't send EXTRA_OUTPUT, which is why it's null. However, some camera apps (like the hero) do. Awesome. So the answer is to specify EXTRA_OUTPUT. The nexus one camera app will save the image to that location. Then in onActivityResult() check if the intent is null. If it isn't, use data.getData(), and if it is then use the location specific in EXTRA_OUTPUT via a constant and insert it into the Mediastore. Urgh.
这篇关于我得到一个NullPointerException异常,当我使用ACTION_IMAGE_CAPTURE拍照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!