Android 添加两个(多个)FileProvider节点(解决头像拍照和第三方SDK(例如融云)拍照的FileProvider冲突问题)

版权声明:旨在技术交流与成长,欢迎大家来交流学习。 https://blog.csdn.net/qq941263013/article/details/81605403

在android7.0以后,修改了对私有存储的限制,导致在获取资源的时候,不能通过Uri.fromFile(...)来获取Uri了;但是在写入数据的时候可以通过Uri.fromFile(...)来获取Uri。Android官方给出的方法是通过FileProvider来解决这一问题。

        但是当此项目中用到了其它包含拍照功能的第三方SDK时,就会导致FileProvider冲突问题,因为第三方SDK为了适配android7.0已经添加了FileProver节点。

        解决方法有两种:

        第一种:创建一个继承自FileProvider的类,并在AndroidManifest中添加provider节点;

        第二种:将第三方SDK中的路径配置copy到provider_paths.xml文件中;

      第一种:

        1.创建ImageFileProver继承FileProvider;

package com.yuyh.library.imgsel;

import android.support.v4.content.FileProvider;

public class ImageFileProvider extends FileProvider {
}

2.在AndroidManifest清单文件的application中添加provider节点;

        <provider
            android:name=".ImageFileProvider"
            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>

3.在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>

第二种:

同样是在res下创建xml文件夹,并在文件夹中创建provider_paths.xml文件,然后将第三方SDK中的路径配置copy到provider_paths.xml文件中;

总结:

以上就是通过FileProvider节点解决拍照冲突的两种方式,如果项目中的头像拍照使用的是开源库,那么推荐使用第一种方式,在以后的新项目中可以直接使用开源库,避免了重复的工作。

猜你喜欢

转载自blog.csdn.net/qq941263013/article/details/81605403