应用内打开文件有时候太麻烦,可以通过调用手机自带软件打开,比如wps、Misrosoft,这样做一是节省了开发时间,二是功能比较多,毕竟人家是专业的。那么如何实现
预览图
/**
* @Description: 打开文件工具类
* @author: ZhangYW
* @time: 2019/1/10 10:52
*/
public class OpenFileUtil {
private static final String[][] MATCH_ARRAY={
//{后缀名, 文件类型}
{".3gp", "video/3gpp"},
{".apk", "application/vnd.android.package-archive"},
{".asf", "video/x-ms-asf"},
{".avi", "video/x-msvideo"},
{".bin", "application/octet-stream"},
{".bmp", "image/bmp"},
{".c", "text/plain"},
{".class", "application/octet-stream"},
{".conf", "text/plain"},
{".cpp", "text/plain"},
{".doc", "application/msword"},
{".docx", "application/msword"},
{".xls", "application/msword"},
{".xlsx", "application/msword"},
{".exe", "application/octet-stream"},
{".gif", "image/gif"},
{".gtar", "application/x-gtar"},
{".gz", "application/x-gzip"},
{".h", "text/plain"},
{".htm", "text/html"},
{".html", "text/html"},
{".jar", "application/java-archive"},
{".java", "text/plain"},
{".jpeg", "image/jpeg"},
{".jpg", "image/jpeg"},
{".js", "application/x-javascript"},
{".log", "text/plain"},
{".m3u", "audio/x-mpegurl"},
{".m4a", "audio/mp4a-latm"},
{".m4b", "audio/mp4a-latm"},
{".m4p", "audio/mp4a-latm"},
{".m4u", "video/vnd.mpegurl"},
{".m4v", "video/x-m4v"},
{".mov", "video/quicktime"},
{".mp2", "audio/x-mpeg"},
{".mp3", "audio/x-mpeg"},
{".mp4", "video/mp4"},
{".mpc", "application/vnd.mpohun.certificate"},
{".mpe", "video/mpeg"},
{".mpeg", "video/mpeg"},
{".mpg", "video/mpeg"},
{".mpg4", "video/mp4"},
{".mpga", "audio/mpeg"},
{".msg", "application/vnd.ms-outlook"},
{".ogg", "audio/ogg"},
{".pdf", "application/pdf"},
{".png", "image/png"},
{".pps", "application/vnd.ms-powerpoint"},
{".ppt", "application/vnd.ms-powerpoint"},
{".prop", "text/plain"},
{".rar", "application/x-rar-compressed"},
{".rc", "text/plain"},
{".rmvb", "audio/x-pn-realaudio"},
{".rtf", "application/rtf"},
{".sh", "text/plain"},
{".tar", "application/x-tar"},
{".tgz", "application/x-compressed"},
{".txt", "text/plain"},
{".wav", "audio/x-wav"},
{".wma", "audio/x-ms-wma"},
{".wmv", "audio/x-ms-wmv"},
{".wps", "application/vnd.ms-works"},
{".xml", "text/plain"},
{".z", "application/x-compress"},
{".zip", "application/zip"},
{"", "*/*"}
};
/**
* 根据路径打开文件
* @param context 上下文
* @param path 文件路径
*/
public static void openFileByPath(Context context, String path) {
if(context==null||path==null)
return;
Intent intent = new Intent();
//设置intent的Action属性
intent.setAction(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_DEFAULT);
//文件的类型
String type = "";
for(int i =0;i < MATCH_ARRAY.length;i++){
//判断文件的格式
if(path.contains(MATCH_ARRAY[i][0])){
type = MATCH_ARRAY[i][1];
break;
}
}
try {
File out = new File(path);
Uri fileURI;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// 由于7.0以后文件访问权限,可以通过定义xml在androidmanifest中申请,也可以直接跳过权限
// 通过定义xml在androidmanifest中申请
// fileURI = FileProvider.getUriForFile(context,
// "com.lonelypluto.zyw_test.provider",
// out);
// 直接跳过权限
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
fileURI = Uri.fromFile(out);
}else{
fileURI = Uri.fromFile(out);
}
//设置intent的data和Type属性
intent.setDataAndType(fileURI, type);
//跳转
if (context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
context.startActivity(intent);
} else {
Toast.makeText(context, "没有找到对应的程序", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) { //当系统没有携带文件打开软件,提示
Toast.makeText(context, "无法打开该格式文件", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
如果通过定义xml在androidmanifest中申请,新建provider_paths.xml在res/xml中
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="sgcg" path=""/>
</paths>
然后在androidmanifest.xml中的application中
<application>
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.lonelypluto.zyw_test.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
...
</application>
最后调用
OpenFileUtil.openFileByPath(content, path);
更多推荐
android根据文件类型调用手机系统软件打开文件
发布评论