Android FileProvider配置和当引用包内已经含有FileProvider的多节点解决办法

在android7.0+,修改了对私有存储的限制,导致在获取资源的时候,不能通过Uri.fromFile来获取uri了我们需要适配7.0+的机型需要这样写:

1:代码适配

 if (Build.VERSION.SDK_INT > 23) {//
                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                Uri contentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileProvider", outputFile);
                intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
            } else {
                intent.setDataAndType(Uri.fromFile(outputFile), "application/vnd.android.package-archive");
            }
情景一 安装更新APP时

Uri fileUri = FileProvider.getUriForFile(context, context.getPackageName()+".fileProvider", file);//android 7.0以上
intent.setDataAndType( fileUri, "application/vnd.android.package-archive" );
intent.addFlags( Intent.FLAG_GRANT_READ_URI_PERMISSION );
context.startActivity( intent );

我们需要获取位于磁盘的apk文件时就需要配置FileProvider

配置说明:

在res目录新建xml文件夹,新建文件取名provider_path.xml

如图             

provider_path.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>

在AndroidManifest.xml文件中加入节点:

<manifest>
....
    <application>
....
          <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities=".provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_path" />
         </provider>
    </application>

</manifest>

到此结束一般情况的配置;

当引用包的AndroidManifest.xml内含有相关配置时,再如此配置就会发生冲突

冲突报错:

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:processDebugManifest'.

......

Caused by: java.lang.RuntimeException: Manifest merger failed with multiple errors, see logs

.....

使用命令行工具Terminal编译执行命令定位详细错误信息:gradlew processDebugManifest --stacktrace

(如果发生编译错误时找不到报错的地方可以试试这个方法)

找到报错信息:

  Attribute provider#android.support.v4.content.FileProvider@authorities value=(com.example.XXX.XXX.XXX) from AndroidManifest.xml:110:13-75
        is also present at [com.ljy.ring:devring:1.0.15] AndroidManifest.xml:20:13-73 value=(com.example.XXX.XXX.XXX.android7.fileprovider).
        Suggestion: add 'tools:replace="android:authorities"' to <provider> element at AndroidManifest.xml:108:9-116:21 to override.
它提示我com.example.XXX.XXX.XXX和com.ljy.ring:devring:1.0.15发生冲突了

现在我们来解决这个冲突:

这是一个取巧的办法,那就是新建一个类MyProvider去继承FileProvider,然后在自己项目中使用这个类;

import android.support.v4.content.FileProvider;

public class MyProvider extends FileProvider {

}

在AndroidManifest.xml中修改:

<provider
    android:name=".MyProvider"
    android:authorities=".myprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_path" />
 </provider>

代码使用中修改:

Uri fileUri = MyProvider.getUriForFile(context, context.getPackageName()+".fileProvider", file);

然后在执行编译   成功

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值