android打开、查看文件
来源:互联网 发布:美橙互联数据库主机名 编辑:程序博客网 时间:2024/05/21 08:02
简单处理原理:
public void openFile0(File file){ String fileName = file.getAbsolutePath();if(fileName.endsWith(".png")|| fileName.endsWith(".jpg")|| fileName.endsWith(".jpeg")|| fileName.endsWith(".gif")){Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "image/*"); startActivity(intent);}else if(fileName.endsWith(".mp4")|| fileName.endsWith(".3gp")|| fileName.endsWith(".avi")|| fileName.endsWith(".flv")|| fileName.endsWith(".wmv")|| fileName.endsWith(".rmvb")|| fileName.endsWith(".asf")|| fileName.endsWith(".mkv")|| fileName.endsWith(".mpg")){Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "video/*"); startActivity(intent);}else if(fileName.endsWith(".mp3")|| fileName.endsWith(".ogg")|| fileName.endsWith(".ape")){Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "audio/*"); startActivity(intent);}else if(fileName.endsWith(".apk")){Intent intent = new Intent();intent.setAction("android.intent.action.VIEW");intent.addCategory("android.intent.category.DEFAULT");intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");startActivity(intent);}}
/**============================================================================**/
/**============================================================================**/
通用性处理关键代码:
/** * 调用可能打开文件的软件打开文件 * @param file */ public void openFile(File file){ 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属性。 intent.setDataAndType(/*uri*/Uri.fromFile(file), type); //跳转 try {startActivity(intent); // 可能无应用可打开, 避免程序崩溃 } catch (Exception e) { Toast.makeText(MainActivity.this, "无应用可打开此文件", Toast.LENGTH_SHORT).show();return; }}
/** * 根据文件后缀名获得对应的MIME类型。 * @param file */ private String getMIMEType(File file) { String type="*/*"; String fName = file.getName(); //获取后缀名前的分隔符"."在fName中的位置。 int dotIndex = fName.lastIndexOf("."); if(dotIndex < 0){ return type; } /* 获取文件的后缀名*/ String end=fName.substring(dotIndex,fName.length()).toLowerCase(Locale.CHINA); if(end=="")return type; //在MIME和文件类型的匹配表中找到对应的MIME类型。 for(int i=0;i<FileFormatParams.MIME_MapTable.length;i++){ if(end.equals(FileFormatParams.MIME_MapTable[i][0])) type = FileFormatParams.MIME_MapTable[i][1]; } return type; }
/** * 文件后缀对应文件类型。 * */ public class FileFormatParams {public static final String MIME_MapTable[][]={ //{后缀名,MIME类型} {".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/vnd.openxmlformats-officedocument.wordprocessingml.document"}, {".xls", "application/vnd.ms-excel"}, {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}, {".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"}, {".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"}, {".prop", "text/plain"}, {".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/x-zip-compressed"}, {"", "*/*"} }; }
0 0
- android打开、查看文件
- Android可以用Html查看器打开txt文件
- android中打开查看pdf文件可用mupdf
- 查看进程打开的文件
- 查看,打开sqlite db文件
- lsof 查看打开文件状态
- Linux打开文件夹,查看文件
- Android 文件打开文件
- android 打开文件
- android 打开文件
- Android打开不同类型文件
- android 打开文件
- Android 文件打开方式
- android 打开文件
- android打开文件方法
- Android 文件打开方式
- Android打开不同类型文件
- android打开文件方法
- CQUPT WEEKLY TRAINING (6)DIV2 解题报告
- HDOJ 5120 Intersection 两圆面积交
- mysql 语句大全
- HDOJ 5119 Happy Matt Friends DP
- unity3d 带缓冲的镜头拉近效果
- android打开、查看文件
- Red5学习笔记(二)连接通信
- 用Java开源项目JOONE实现人工智能编程
- strust2初识及示例
- HDOJ 5122 K.Bro Sorting 水题
- 如何让登陆后直接转跳到首页而不是管理中心解决办法
- I/O 输入输出
- 邻接表的深度优先搜索(DFS)
- nginx正则笔记