Android下载apk文件,安装不上

对于Android版本的坑,已经无力吐槽了,由于高版本增加了权限,所以再用原始的安装apk文件的代码,只会碰坑。。。

简要概括:有可能有的小伙伴,按照新版本的规则写完代码,build的时候,会报一个错误:Error:Execution failed for task ':app:processDebugManifest'.> Manifest merger failed with multiple errors, see logs,这是由于与现有的jar包中的重名了。解决办法:

 

步骤1:

建一个类继承FileProvider  :
 

public class SLFileProvider extends FileProvider {
}

然后在AndroidManifest.xml里声明:

<provider
    android:name=".common.utils.SLFileProvider"
    android:authorities="com.android.sl.restaurant.fileProvider"
    android:grantUriPermissions="true"
    android:exported="false">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

 

步骤2:

在res目录下新增file_paths.xml文件

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

步骤3:

  //安装apk文件
    private void installApk(String filename) {
     
        Intent intent = new Intent(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        /* Android N 写法*/
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".fileProvider", new File(filename));
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
        /* Android N之前的老版本写法*/
            intent.setDataAndType(Uri.fromFile(new File(filename)), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        startActivity(intent);


//        File file = new File(filename);
//        Intent intent = new Intent();
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        intent.setAction(Intent.ACTION_VIEW);     //浏览网页的Action(动作)
//        String type = "application/vnd.android.package-archive"; //apk文件类型
//        intent.setDataAndType(Uri.fromFile(file), type);  //设置数据类型
//        startActivity(intent);
    }

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值