源头

都是因为一个bug

android.os.FileUriExposedException:

file:///storage/emulated/0/mwh/app.apk exposed beyond app through Intent.getData()

复制代码

因为这个bug 显示 原来的 apk安装方法无法在高于7.0 的手机系统上安装apk了。

所以就得需要使用FileProvider 来做兼容

安装apk 兼容7.0以上系统

配置mainfest 信息

在mainfest 的 application 中 添加如下代码

android:name="android.support.v4.content.FileProvider"

android:authorities="${applicationId}.fileprovider"

android:grantUriPermissions="true"

android:exported="false">

android:name="android.support.FILE_PROVIDER_PATHS"

android:resource="@xml/file_paths" />

复制代码android:authorities 这里使用的是包名。这个其实可以随便写,写成123 都可以。

android:resource 这里配置一个 xml文件。名字也可以随便写 这里就叫 file_paths好了

配置xml 信息

455ef75ebd63085d5e9185c6e56e5f7b.png

这里配置的很简单 ,

external-path 是放到sd卡的目录下 Environment.getExternalStorageDirectory()

path="." 代表共享 sd卡下的所有目录。手机会遍历sd卡下的所有目录,来匹配你要安装 的apk 的目录。。

name="." 代表 你apk存放目录下所有apk的名字都会遍历一遍,然后跟你要安装的apk进行匹配

复制代码

安装apk代码

/**

* 安装apk

*

* @param fileSavePath

*/

private void installApk(String fileSavePath) {

File file = new File(fileSavePath);

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Uri data;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//判断版本大于等于7.0

// "sven.com.fileprovider.fileprovider"即是在清单文件中配置的authorities

// 通过FileProvider创建一个content类型的Uri

data = FileProvider.getUriForFile(this, "sven.com.fileprovider.fileprovider", file);

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);// 给目标应用一个临时授权

} else {

data = Uri.fromFile(file);

}

intent.setDataAndType(data, "application/vnd.android.package-archive");

startActivity(intent);

}

复制代码

##其他

为了演示效果。我把事先准备好的apk。放到了assets 目录下

点击安装 按钮,会把apk 赋值到 指定目录下,然后在 进行安装操作。

/**

* 如果sdcard没有文件就复制过去

*/

private String copyFile() {

AssetManager assetManager = this.getAssets();

String newFilePath = Environment.getExternalStorageDirectory() +"/mwh/app-release.apk";

String Path = Environment.getExternalStorageDirectory() + "/mwh";

try {

File file1 = new File(Path);

if (!file1.exists()) {

file1.mkdir();

}

File file = new File(newFilePath);

if (!file.exists()) {//文件不存在才复制

InputStream in = assetManager.open("app-release.apk");

OutputStream out = new FileOutputStream(newFilePath);

byte[] buffer = new byte[1024];

int read;

while ((read = in.read(buffer)) != -1) {

out.write(buffer, 0, read);

}

in.close();

out.flush();

out.close();

}

} catch (Exception e) {

Log.e(TAG, e.getMessage());

}

return newFilePath;

}

复制代码注意

String newFilePath = Environment.getExternalStorageDirectory() +"/mwh/app-release.apk";

复制代码

这里的目录是 sd 卡目录下的mwh 文件夹。我们直接使用

就可以了。

复制代码

最后

到这里就结束了。 不懂的可以把代码下载下来 试一下

https://github.com/wanghao200906/FileProviderDemo

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐