相机无法在Nougat 7.0中使用 [英] Camera is not working in Nougat 7.0

查看:56
本文介绍了相机无法在Nougat 7.0中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的相机代码可在所有Android版本上使用,但在Nougat 7.0中会出现以下错误:

My camera code is working in all Android versions but in Nougat 7.0 it gives the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference


上调用虚拟方法'java.lang.String android.net.Uri.toString()'

除了在android 7.0上,它在所有其他版本的android上都能正常运行。我已授予摄像机&的运行时权限画廊,但相机仍无法正常工作。以下是相关代码:

It is working perfectly on all other versions of android except on android 7.0. I have given run time permission for the camera & the gallery but the camera is still not working. Here is the relevant code:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {
        if(requestCode == Constants.CROPPED_PIC_REQUEST_CODE){
            CropImage.ActivityResult result = (CropImage.ActivityResult) data.getExtras().get(CropImage.CROP_IMAGE_EXTRA_RESULT);
            Uri selectedImageUri = result == null ? null : result.getUri();
            Bitmap bitmap = null;
            Log.d("SetUpProfile","Uri cropped is "+outputFileUri);
            bitmap = getBitmap(selectedImageUri);
    //                    bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
            circleImageView.setImageBitmap(bitmap);
            finalBitmap = bitmap;
        }
        else if (requestCode == Constants.YOUR_SELECT_PICTURE_REQUEST_CODE) {
            final boolean isCamera;
            if (data == null) {
                isCamera = true;
            } else {
                final String action = data.getAction();
                if (action == null) {
                    isCamera = false;
                } else {
                    isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                }
            }

            Uri selectedImageUri;
            if (isCamera) {
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
                String value = prefs.getString("path", "error");
                selectedImageUri = Uri.parse(value);
            } else {
                selectedImageUri = data == null ? null : data.getData();
            }

            Intent i = new Intent(Five.this,CropImageActivity.class);
            i.putExtra("ImageURI", selectedImageUri.toString());
            startActivityForResult(i,Constants.CROPPED_PIC_REQUEST_CODE);

        }
    }
}

我的日志猫:-

FATAL EXCEPTION: main
Process: com.sancsvision.wayndr, PID: 31570
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=11, result=-1, data=Intent {  }} to activity {com.sancsvision.wayndr/com.sancsvision.wayndr.Five}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference                     
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4053)              
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096)        
    at android.app.ActivityThread.-wrap20(ActivityThread.java)                      
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)         
    at android.os.Handler.dispatchMessage(Handler.java:102)                     
    at android.os.Looper.loop(Looper.java:154)                      
    at android.app.ActivityThread.main(ActivityThread.java:6077)                        
    at java.lang.reflect.Method.invoke(Native Method)                       
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)      
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)                                                     
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
    at com.sancsvision.wayndr.Five.onActivityResult(Five.java:259)
    at android.app.Activity.dispatchActivityResult(Activity.java:6917)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4049)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096) 
    at android.app.ActivityThread.-wrap20(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6077) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)


推荐答案

尝试此操作并非是一旦拍摄照片并保存到SD卡后便会产生问题在Nougat中获取URI有所不同。...

Try this its not the intent that create the problem once you take the picture and save to the SD card and getting back the URI is different in Nougat....

在您的应用程序上实现FileProvider很容易。首先,您需要在AndroidManifest.xml中的以下标记下添加FileProvider标记:AndroidManifest.xml

It is quite easy to implement FileProvider on your application. First you need to add a FileProvider tag in AndroidManifest.xml under tag like below: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    <application
        ...
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>
    </application>
</manifest>

然后在res文件夹下的xml文件夹中创建provider_paths.xml文件。如果文件夹不存在,则可能需要创建该文件夹。

And then create a provider_paths.xml file in xml folder under res folder. Folder may be needed to create if it doesn't exist.

res / xml / provider_paths.xml

res/xml/provider_paths.xml

 <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <external-path name="external_files" path="."/>
    </paths>

完成!现在已声明FileProvider并可以使用。

Done! FileProvider is now declared and be ready to use.

最后一步是在MainActivity.java中更改下面的代码行

The final step is to change the line of code below in MainActivity.java

 Uri photoURI = Uri.fromFile(createImageFile());

Uri photoURI = FileProvider.getUriForFile(MainActivity.this,
        BuildConfig.APPLICATION_ID + ".provider",
        createImageFile());

然后...完成!现在,您的应用程序应该可以在包括Android Nougat在内的任何Android版本上正常运行。干杯!

And .... done ! Your application should now work perfectly fine on any Android version including Android Nougat. Cheers !

这篇关于相机无法在Nougat 7.0中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆