欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-12-24 08:33:02
解决方案类似: Android项目实战(四十):Andoird 7.0+ 安装APK适配 解决方法: 一、在AndroidManifest.xml 文件中添加 四大组件之一的 注意这里的 android :authorities 属性的值 中的 com.xxx.xxxx 是你的 ......

解决方案类似:

android项目实战(四十):andoird 7.0+ 安装apk适配

 

解决方法:

 

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

    

Android项目实战(四十九):Andoird 7.0+相机适配
 <!-- 适配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项目实战(四十九):Andoird 7.0+相机适配

 

   注意这里的  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);