当我尝试捕获图像(内置相机)并将其保存到文件时出现 NullPointerException [重复]
Posted
技术标签:
【中文标题】当我尝试捕获图像(内置相机)并将其保存到文件时出现 NullPointerException [重复]【英文标题】:NullPointerException when I try to capture an image (built-in camera) and save it to a file [duplicate] 【发布时间】:2014-12-11 05:44:49 【问题描述】:这是应用程序所做的 - 用户单击 Activity A 上的按钮(拍照),捕获的图像被设置为 Activity A 中的 ImageView,然后用户单击“保存”按钮,这会将他/她到 Activity B,在那里他们拍摄的图像被显示(在 Activity B 的 ImageView 上)
为了做到这一点,我试图找到一种方法来保存图像。我尝试了很多不同的方法,但我不断收到 NullPointerException。这是我的代码:
我突出显示了我认为错误所在的区域:
public void onClick(View view)
switch (view.getId())
case R.id.take_picture_button:
takePic = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (takePic.resolveActivity(getPackageManager()) != null)
File photoFile = null;
try
photoFile = createImageFile(); //so photoFile = the file we created up top
catch (IOException ex)
// Error occurred while creating the File
ex.printStackTrace();
// Continue only if the File was successfully created
if (photoFile != null)
**takePic.putExtra(MediaStore.EXTRA_OUTPUT**, //extra_output is just the name of the Intent-extra used to indicate a content resolver Uri to be used to store the requested image or video.
**Uri.fromFile(photoFile));**
startActivityForResult(takePic, cameraData);
这是 onActivityResult 的代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == cameraData && resultCode == RESULT_OK)
mReturningWithResult = true;
extras = data.getExtras();
在这里我将图像视图设置为捕获的图像(在活动 A 上)
@Override
protected void onPostResume()
super.onPostResume();
if (mReturningWithResult)
foodImage = (Bitmap) extras.get("data");
foodImageView.setImageBitmap(foodImage);
mReturningWithResult = false;//resetting it for next time
这是 logcat(对不起,我是新手)
10-15 20:58:59.612 32005-32005/com.example.nikhil.foodshark D/OpenGLRenderer﹕ Enabling debug mode 0
10-15 20:59:21.545 32005-32005/com.example.nikhil.foodshark I/PersonaManager﹕ getPersonaService() name persona_policy
10-15 20:59:23.127 32005-32005/com.example.nikhil.foodshark W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
10-15 20:59:33.678 32005-32005/com.example.nikhil.foodshark D/AndroidRuntime﹕ Shutting down VM
10-15 20:59:33.678 32005-32005/com.example.nikhil.foodshark W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41a39da0)
10-15 20:59:33.678 32005-32005/com.example.nikhil.foodshark E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.nikhil.foodshark, PID: 32005
java.lang.RuntimeException: Failure delivering result ResultInfowho=null, request=1, result=-1, data=null to activity com.example.nikhil.foodshark/com.example.nikhil.foodshark.NewDish: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3680)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3723)
at android.app.ActivityThread.access$1400(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.nikhil.foodshark.NewDish.onActivityResult(NewDish.java:144)
at android.app.Activity.dispatchActivityResult(Activity.java:5650)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3676)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3723)
at android.app.ActivityThread.access$1400(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
10-15 20:59:56.353 32005-32005/com.example.nikhil.foodshark I/Process﹕ Sending signal. PID: 32005 SIG: 9
10-15 20:59:56.613 378-378/com.example.nikhil.foodshark I/PersonaManager﹕ getPersonaService() name persona_policy
10-15 20:59:56.723 378-378/com.example.nikhil.foodshark I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_KK_2.7_RB1.04.04.02.007.050_msm8960_refs/tags/AU_LINUX_ANDROID_KK_2.7_RB1.04.04.02.007.050__release_AU ()
OpenGL ES Shader Compiler Version: 17.01.12.SPL
Build Date: 03/28/14 Fri
Local Branch:
Remote Branch: refs/tags/AU_LINUX_ANDROID_KK_2.7_RB1.04.04.02.007.050
Local Patches: NONE
Reconstruct Branch: NOTHING
10-15 20:59:56.763 378-378/com.example.nikhil.foodshark D/OpenGLRenderer﹕ Enabling debug mode 0
你们知道我可以解决这个问题的方法吗?谢谢!
【问题讨论】:
什么是空指针异常?请发布堆栈跟踪。 不要说"..我认为错误在哪里"。堆栈跟踪准确地告诉错误在哪里。在这里张贴 我没有看到任何突出显示的区域,我只看到 cmets 还发布了您的堆栈跟踪,以便我们提供帮助 嘿,对不起,我是新手 - 我发布了 logcat 什么是 NewDish 第 144 行? 【参考方案1】:您正在传递MediaStore.EXTRA_OUTPUT
,在这种情况下,onActivityResult
中的intent
字段可以为空。
解决方案是只使用您创建的照片文件,并在putExtra
中传递为uri
因此,与其尝试从意图中获取照片文件位置,不如使用Uri.fromFile(photoFile)
替换
extras = data.getExtras();
...
与
`Uri.fromFile(photoFile)`
或者只是使用photoFile
,如果这是你想要的......只是不要依赖意图data
【讨论】:
感谢 ashoke 以及所有提供帮助的人 - 我想我现在明白了!以上是关于当我尝试捕获图像(内置相机)并将其保存到文件时出现 NullPointerException [重复]的主要内容,如果未能解决你的问题,请参考以下文章
保存用相机拍摄或从相机胶卷中选择的图像时出现长时间延迟 - iPhone
尝试在 Windows Phone 上显示相机胶卷中的图像时出现跨线程异常