Android 開發工具類 34_OpenFileUtil

來源:互聯網
上載者:User

標籤:

匹配檔案尾碼名 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 }

 

Android 開發工具類 34_OpenFileUtil

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.