打开文档
private void openFile(File file) { try { Log.e("文件名", file.getName()); Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //设置intent的Action属性 intent.setAction(Intent.ACTION_VIEW); //获取文件file的MIME类型 String type = getMIMEType(file); //设置intent的data和Type属性。android 7.0以上crash,改用provider if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { Uri fileUri = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".provider", file);//android 7.0以上 intent.setDataAndType(fileUri, type); grantUriPermission(mContext, fileUri, intent); } else { intent.setDataAndType(/*uri*/Uri.fromFile(file), type); } //跳转 mContext.startActivity(intent); } catch (Exception e) { e.printStackTrace(); toastLong("没有支持打开文档的应用"); } } private static void grantUriPermission(Context context, Uri fileUri, Intent intent) { List resInfoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); for (ResolveInfo resolveInfo : resInfoList) { String packageName = resolveInfo.activityInfo.packageName; context.grantUriPermission(packageName, fileUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); } } /** * 根据文件后缀名获得对应的MIME类型。 * * @param file */ private static String getMIMEType(File file) { String type = "*/*"; String fName = file.getName(); //获取后缀名前的分隔符"."在fName中的位置。 int dotIndex = fName.lastIndexOf("."); if (dotIndex
在Manifest.xml中配置
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="包名.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
在资源文件中新建xml文件file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="files_root"
path="Android/data/com.dfy.ming.lawyer/"/>
<external-path
name="external_storage_root"
path="."/>
</paths>
对于8.0安装:
//替换安装方法
void update() {
if (Build.VERSION.SDK_INT >= 26) {
boolean b = getPackageManager().canRequestPackageInstalls();
if (b) {
installApk();//安装应用的逻辑(写自己的就可以)
} else {
//请求安装未知应用来源的权限
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
startActivityForResult(intent, 10086);
}
} else {
installApk();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == 10086) {
installApk();//再次执行安装流程,包含权限判等
}
}
private void installApk() {
File apkFile = new File(RyhuiApp.path);
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//provider authorities
Uri apkUri = FileProvider.getUriForFile(context.getApplicationContext(), "com.finance.ryhui.pepe.fileProvider", apkFile);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
}
context.startActivity(intent);
}