当我尝试捕获图像(内置摄像头)并将其保存到文件时出现NullPointerException

时间:2014-10-16 00:56:07

标签: java android nullpointerexception camera android-camera-intent

这里有应用程序的作用 - 用户点击活动A上的按钮(拍照),捕获的图像设置为活动A中的ImageView,然后用户点击"保存& #34;按钮,将他/她带到活动B,在那里显示他们拍摄的图像(在活动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 ResultInfo{who=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

你们知道我可以解决这个问题吗?谢谢!

1 个答案:

答案 0 :(得分:0)

您正在传递MediaStore.EXTRA_OUTPUT,在这种情况下,intent中的onActivityResult字段可以为空。

解决方案是使用您创建的照片文件,并在uri

中作为putExtra传递

因此,不要尝试从意图中获取照片文件位置,只需使用Uri.fromFile(photoFile)

即可

替换

    extras = data.getExtras();
    ...

   `Uri.fromFile(photoFile)`

或者只是使用photoFile,如果这是你想要的......只是不依赖于意图data