android - FileProvider 如何在 provider_path.xml 中选择/区分提供者路径?
问题描述
我必须使用 FileProvider 来安装 apk。现在一切正常,但我并不真正理解 FileProvider 为了选择在provider_paths.xml中定义的正确路径而遵循的行为。这是我的代码:
清单.xml
<provider
android:name="android.support.v4.content.FileProvider"
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>
main_activity.java
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
intent = new Intent(Intent.ACTION_VIEW);
File apk = new File("/sdcard/example.apk");
String fileType = "application/vnd.android.package-archive";
intent.setDataAndType(Uri.fromFile(apk), fileType);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} else {
intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
File directory = new File(Environment.getExternalStorageDirectory().toString());
File apk = new File(directory, "example.apk");
Uri fileUri = FileProvider.getUriForFile(this, valoresGenerales.appContext.getPackageName() + ".provider", apk);
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(apk.getName()));
intent.setDataAndType(fileUri, type);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
startActivity(intent);
provider_paths.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="." />
<files-path name="files_path" path="Download"/>
</paths>
使 FileProvider 在external_path或files_path(或文件中定义的任何内容)之间进行选择取决于什么?
解决方案
推荐阅读
- javascript - 将 sweetalert 与 Vue.js 一起使用时的问题
- visual-studio-code - 我无法在 Manjaro 上启动 Visual Studio 代码
- typescript - asyncData 和 fetch 没有设置数据
- php - 如何在 Laravel 中使用子文件夹中的自定义类?
- javascript - 固定导航栏与锚标签重叠
- python - 导入 rsa 私钥并加密字符串
- java - 正则表达式 - 查找某个字符出现的所有数字
- elixir - 如果嵌入模式中已经存在一个项目,我如何让这个函数更新它的数量并且不将该项目添加到列表中?
- swift - SwiftUI 自动调整底页大小
- apache-nifi - 如何在 cloudera manager 上更改 NiFi 属性