Android 调用相机出现异常 NullPointerException: uri

碰到个很玄学的问题,今天项目需要调用摄像头,由于之前使用过,直接从原有项目copy了。

异常信息

java.lang.RuntimeException: Unable to resume activity {com.example.androidx/com.example.androidx.activity.FaceDatabaseManage}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.androidx/com.example.androidx.activity.FaceDatabaseManage}: java.lang.NullPointerException: uri
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4430)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4470)
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:51)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
        at android.os.Handler.dispatchMessage(Handler.java:112)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
     Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.androidx/com.example.androidx.activity.FaceDatabaseManage}: java.lang.NullPointerException: uri
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5078)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4409)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4470) 
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:51) 
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199) 
        at android.os.Handler.dispatchMessage(Handler.java:112) 
        at android.os.Looper.loop(Looper.java:216) 
        at android.app.ActivityThread.main(ActivityThread.java:7625) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987) 
     Caused by: java.lang.NullPointerException: uri
        at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:128)
        at android.content.ContentResolver.openInputStream(ContentResolver.java:1016)
        at com.example.androidx.activity.FaceDatabaseManage.onActivityResult(FaceDatabaseManage.java:246)
        at android.app.Activity.dispatchActivityResult(Activity.java:7797)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5071)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4409) 
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4470) 
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:51) 
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199) 
        at android.os.Handler.dispatchMessage(Handler.java:112) 
        at android.os.Looper.loop(Looper.java:216) 
        at android.app.ActivityThread.main(ActivityThread.java:7625) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987) 

代码相关

调用相机的代码

 private void startCamaraForRegister() {
        // 创建File对象,用于存储拍照后的图片
        File outputImage = new File(getExternalCacheDir(), "output_image.jpg");
        try {
            if (outputImage.exists()) {
                outputImage.delete();
            }
            outputImage.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (Build.VERSION.SDK_INT < 24) {
            imageUri = Uri.fromFile(outputImage);
        } else {
            imageUri = FileProvider.getUriForFile(FaceDatabaseManage.this, AUTHORITY, outputImage);

        }
        // 启动相机程序
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        System.out.println(imageUri==null);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        startActivityForResult(intent, TAKE_PHOTO);

    }

在这里我打印了imageUri,这里显示不是空,从而执行 startActivityForResult方法

@Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        System.out.println(imageUri==null);
        System.out.println("11111111");

        switch (requestCode) {
            case TAKE_PHOTO:
                if (resultCode == RESULT_OK) {
                    try {
                        //将拍摄的照片显示到头像中
                        Bitmap bitmap = BitmapFactory.decodeStream(FaceDatabaseManage.this.getContentResolver().openInputStream(imageUri));
                      
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                break;
          
            default:
                break;
        }
    }

竟然提示imageUri是空的,明明我们先获取了imageUri ,并且打印显示不是空的,难道我的全局变量改变了?
更玄学的是,横屏报错,正常竖屏就没问题!!! 于是我就去stackflow上逛了半天

碰见一个跟我一样的问题
Android 调用相机出现异常 NullPointerException: uri_第1张图片
https://stackoverflow.com/questions/20424909/android-startcamera-gives-me-null-intent-and-does-it-destroy-my-global-varia

解决方案:

imanfest文件中,在对应的activity 加

android:windowSoftInputMode="adjustResize"  
android:configChanges="orientation|keyboardHidden|screenSize"

解决

你可能感兴趣的:(Android)