Android 开发工具类 34_OpenFileUtil

匹配文件后缀名 MIME 类型。

  1 import java.io.File;
  2 
  3 import android.content.Context;
  4 import android.content.Intent;
  5 import android.net.Uri;
  6 
  7 public class OpenFileUtil {
  8 
  9     public static final String[][] MIME_MapTable = {
 10             // {后缀名,MIME类型}
 11             { ".3gp", "video/3gpp" },
 12             { ".apk", "application/vnd.android.package-archive" },
 13             { ".asf", "video/x-ms-asf" },
 14             { ".avi", "video/x-msvideo" },
 15             { ".bin", "application/octet-stream" },
 16             { ".bmp", "image/bmp" },
 17             { ".c", "text/plain" },
 18             { ".class", "application/octet-stream" },
 19             { ".conf", "text/plain" },
 20             { ".cpp", "text/plain" },
 21             { ".doc", "application/msword" },
 22             { ".docx",
 23                     "application/vnd.openxmlformats-officedocument.wordprocessingml.document" },
 24             { ".xls", "application/vnd.ms-excel" },
 25             { ".xlsx",
 26                     "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" },
 27             { ".exe", "application/octet-stream" },
 28             { ".gif", "image/gif" },
 29             { ".gtar", "application/x-gtar" },
 30             { ".gz", "application/x-gzip" },
 31             { ".h", "text/plain" },
 32             { ".htm", "text/html" },
 33             { ".html", "text/html" },
 34             { ".jar", "application/java-archive" },
 35             { ".java", "text/plain" },
 36             { ".jpeg", "image/jpeg" },
 37             { ".jpg", "image/jpeg" },
 38             { ".js", "application/x-javascript" },
 39             { ".log", "text/plain" },
 40             { ".m3u", "audio/x-mpegurl" },
 41             { ".m4a", "audio/mp4a-latm" },
 42             { ".m4b", "audio/mp4a-latm" },
 43             { ".m4p", "audio/mp4a-latm" },
 44             { ".m4u", "video/vnd.mpegurl" },
 45             { ".m4v", "video/x-m4v" },
 46             { ".mov", "video/quicktime" },
 47             { ".mp2", "audio/x-mpeg" },
 48             { ".mp3", "audio/x-mpeg" },
 49             { ".mp4", "video/mp4" },
 50             { ".mpc", "application/vnd.mpohun.certificate" },
 51             { ".mpe", "video/mpeg" },
 52             { ".mpeg", "video/mpeg" },
 53             { ".mpg", "video/mpeg" },
 54             { ".mpg4", "video/mp4" },
 55             { ".mpga", "audio/mpeg" },
 56             { ".msg", "application/vnd.ms-outlook" },
 57             { ".ogg", "audio/ogg" },
 58             { ".pdf", "application/pdf" },
 59             { ".png", "image/png" },
 60             { ".pps", "application/vnd.ms-powerpoint" },
 61             { ".ppt", "application/vnd.ms-powerpoint" },
 62             { ".pptx",
 63                     "application/vnd.openxmlformats-officedocument.presentationml.presentation" },
 64             { ".prop", "text/plain" }, { ".rc", "text/plain" },
 65             { ".rmvb", "audio/x-pn-realaudio" }, { ".rtf", "application/rtf" },
 66             { ".sh", "text/plain" }, { ".tar", "application/x-tar" },
 67             { ".tgz", "application/x-compressed" }, { ".txt", "text/plain" },
 68             { ".wav", "audio/x-wav" }, { ".wma", "audio/x-ms-wma" },
 69             { ".wmv", "audio/x-ms-wmv" },
 70             { ".wps", "application/vnd.ms-works" }, { ".xml", "text/plain" },
 71             { ".z", "application/x-compress" },
 72             { ".zip", "application/x-zip-compressed" }, { "", "*/*" } };
 73 
 74     /**
 75      * 打开文件
 76      * 
 77      * @param file
 78      */
 79     public static void openFile(File file, Context context) {
 80 
 81         Intent intent = new Intent();
 82         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 83         // 设置 intent 的 Action 属性
 84         intent.setAction(Intent.ACTION_VIEW);
 85         // 获取文件 file 的 MIME 类型
 86         String type = getMIMEType(file);
 87         // 设置 intent 的 data 和 Type 属性。
 88         intent.setDataAndType(/* uri */Uri.fromFile(file), type);
 89         // 跳转
 90         context.startActivity(intent);
 91 
 92     }
 93 
 94     /**
 95      * @param file
 96      * @return 获得文件后缀名
 97      */
 98     private static String getMIMEType(File file) {
 99         String type = "*/*";
100         String fName = file.getName();
101         // 获取后缀名前的分隔符"."在fName中的位置。
102         int dotIndex = fName.lastIndexOf(".");
103         if (dotIndex < 0) {
104             return type;
105         }
106         /* 获取文件的后缀名 */
107         String end = fName.substring(dotIndex, fName.length()).toLowerCase();
108         if (end == "")
109             return type;
110         // 在 MIME 和文件类型的匹配表中找到对应的 MIME 类型。
111         for (int i = 0; i < MIME_MapTable.length; i++) { // MIME_MapTable??在这里你一定有疑问,这个MIME_MapTable是什么?
112             if (end.equals(MIME_MapTable[i][0]))
113                 type = MIME_MapTable[i][1];
114         }
115         return type;
116     }
117 
118 }
原文地址:https://www.cnblogs.com/renzimu/p/4688664.html