Android项目实战(四十九):Andoird 7.0+相机适配

解决方案类似:

Android项目实战(四十):Andoird 7.0+ 安装APK适配

 

解决方法:

 

一、在AndroidManifest.xml 文件中添加 四大组件之一的 <provider>

    

复制代码
 <!-- 适配7.0 apk安装 -->
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="你的包名.fileprovider" 
            android:grantUriPermissions="true"
            android:exported="false">
            <!--元数据-->
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
复制代码

 

   注意这里的  android :authorities 属性的值 中的 com.xxx.xxxx 是你的包名,不可随意填写

 

二、res 目录下 建一个xml 文件,并新建xml文件file_paths.xml 

    注意文件名要和第一步中的 resource 属性的值一致 

    内容为:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path path="." name="download"/>
</paths>

 

 

三、根据机型的Android系统级别执行不同的安装调用相机Intent代码

     注意,根据系统版本执行不同代码,7.0以下调用7.0+的代码会报错,7.0+的调用7.0以下的会报错。

 

      File cameraFile = new File(PathUtil.getInstance().getImagePath(), 
                + System.currentTimeMillis() + ".jpg");
 
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            intent.putExtra(MediaStore.EXTRA_OUTPUT,
                    FileProvider.getUriForFile(getActivity(),"你的包名.fileprovider", cameraFile));
        }else {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraFile));
        }
        startActivityForResult(intent, REQUEST_CODE_CAMERA);

 

  

posted @ 2018-08-23 18:51  听着music睡  阅读(663)  评论(0编辑  收藏  举报