1.适配Intent打开文件
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.tmgp.sgame.conan.myapplication.fileprovider"
android:grantUriPermissions="true"
android:exported="false"
>
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path path="Android/data/com.tmgp.sgame.conan.myapplication/" name="files_root" />
<external-path path="." name="external_storage_root" />
</paths>
File file = new File("/storage/emulated/0/1/1.apk");
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(MainActivity.this,"com.tmgp.sgame.conan.myapplication.fileprovider", file);
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
if (getPackageManager().queryIntentActivities(intent, 0).size() > 0) {
startActivity(intent);
}
File file = new File("/storage/emulated/0/1/1.zip");
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(MainActivity.this,"com.tmgp.sgame.conan.myapplication.fileprovider", file);
//打开音频资源audio,视频资源video,图片资源image,文本资源text,所有能打开应用资源application
intent.setDataAndType(contentUri, "application/*");
} else {
intent.setDataAndType(Uri.fromFile(file), "application/*");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
if (getPackageManager().queryIntentActivities(intent, 0).size() > 0) {
startActivity(intent);
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。