首先我们先封装一个FileProvider7Utils工具类,代码如下:
1.首先进行当前版本进行判断
public static Uri getUriForFile(Context context, File file)
{
Uri fileUri=null;
if(Build.VERSION.SDK_INT>=24)
{
fileUri=getUriForFile24(context,file);
}
else{
fileUri = Uri.fromFile(file);
}
return fileUri;
}
2.利用FileProvider进行路径转换
public static Uri getUriForFile24(Context context,File file)
{
Uri fileUri=android.support.v4.content.FileProvider.getUriForFile(context,context.getPackageName()
+".android7.fileprovider",file);
return fileUri;
}
3.在res下新建xml文件再新建file_path.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<root-path
name="root"
path="" />
<files-path
name="files"
path="" />
<cache-path
name="cache"
path="" />
<external-path
name="external"
path="" />
<external-files-path
name="external_file_path"
path="" />
<external-cache-path
name="external_cache_path"
path="" />
</paths>
4.manifest注册provider
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.android7.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_path" />
</provider>
4.打开work文档
if(filePath.endsWith(".docx")||filePath.endsWith(".doc"))
{
Uri uriForFile = FileProvider7Utils.getUriForFile(mContext, file);
Intent openWord = OfficeUtils.getOpenWord(uriForFile);
mContext.startActivity(openWord);
downloadDialog.dismiss();
}
5.officeUtils工具类,切记android7.0以上必须添加“ intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);”,否则将无法打开
public static Intent getOpenWord( Uri uriForFile)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
// Uri uriForFile = FileProvider7Utils.getUriForFile(mContext, apkfile);
Log.i("Uri",uriForFile+"");
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N)
{
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
intent.setDataAndType(uriForFile,"application/msword");
return intent;
}
6.同理打开excel
//android获取一个用于打开Excel文件的intent
public static Intent getExcelFileIntent(Uri file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N)
{
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
intent.setDataAndType(file, "application/vnd.ms-excel");
return intent;
}
7.//android获取一个用于打开PDF文件的intent
public static Intent getPdfFileIntent(Uri file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N)
{
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
intent.setDataAndType(file, "application/pdf");
return intent;
}
end:android打开第三方的应用我就不一一列举了,道理肯定都是一样的。我写这篇博客的目的:一是将自己在工作中的问题记录下来以后好查阅;二是需要帮助到像我这类的小菜鸟们,如有不妥之处,请各位大佬多多指教,望共勉之!