【发布时间】:2019-01-23 09:33:12
【问题描述】:
我想从存储中打开并安装一个 apk 文件,我使用了以下代码:
File fileToOpen = new File(path);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = path.substring(path.lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(ext);
if (fileToOpen.exists()) {
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", fileToOpen);
intent.setDataAndType(uri, type);
} else {
intent.setDataAndType(Uri.fromFile(fileToOpen), type);
}
context.startActivity(intent);
}
在我的 manifest 文件中:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<provider
android:authorities="${applicationId}.provider"
android:name="android.support.v4.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_path" />
</provider>
我的 provider_path.xml:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path path="." name="external_files" />
</paths>
我必须在运行时请求权限并接受,但在我调用此方法后,什么也没有发生。文件已经存在,我的代码中发生了什么?请帮我找到它,非常感谢:((
【问题讨论】:
标签: android android-fileprovider