android - 找不到包含路径的已配置根目录
问题描述
执行时出现错误:
@SuppressLint("SetWorldReadable")
public void installAPK(File dest) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
dest.setReadable(true, false);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(dest), "application/vnd.android.package-archive");
this.getApplicationContext().startActivity(intent);
} else {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri fileUri = FileProvider.getUriForFile(this,
"com.developerfromjokela.fileprovider",
dest);
intent.setDataAndType(fileUri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
}
我得到:
09-22 11:11:12.866 10506-10551/com.developerfromjokela.pusahub E/AndroidRuntime: FATAL EXCEPTION: Thread-5
Process: com.developerfromjokela.pusahub, PID: 10506
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/PusaHub/PusaStore/.downloads/PusaCloud_20180922_111100.apk
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:738)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:417)
at com.developerfromjokela.pusahub.AppDetailsActivity.installAPK(AppDetailsActivity.java:412)
at com.developerfromjokela.pusahub.AppDetailsActivity.lambda$downloadFile$3$AppDetailsActivity(AppDetailsActivity.java:329)
at com.developerfromjokela.pusahub.AppDetailsActivity$$Lambda$1.run(Unknown Source:30)
at java.lang.Thread.run(Thread.java:764)
我很早就在stackoverflow中搜索并发现了问题,但这些并不能解决我的问题。我认为这与之前提出的问题有所不同。
安卓清单:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.developerfromjokela.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
文件路径 XML:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="files"
path="/" />
</paths>
解决方案
用这个:
对于N
版本
File file = new File(filePathUri.getPath());
Uri photoUri = FileProvider.getUriForFile(getActivity(),getActivity().getPackageName() + ".provider", file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
推荐阅读
- c# - 如何在 ASP.NET 中执行之前在 api 中拦截 GET 请求?
- javascript - 是否可以在上传前同时验证文件大小和扩展名?
- excel - 协调/比较两个表的数据
- python - 替换熊猫数据框中包含引号的字符串
- typescript - 在 image.onload 函数内部,访问类对象和图像对象
- iis - 如何使用 Windows 性能监视器监视 IIS 工作进程
- google-cloud-storage - 客户提供的加密密钥功能不可用(但它是!)
- highcharts - Highcharts:我们如何对动画系列进行排序?
- javascript - 无法设置多张图片
- regex - 如果文本被换行符或标签打断,如何替换文本?