Android開啟不同類型檔案

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   os   使用   ar   

轉自http://www.cnblogs.com/hibraincol/archive/2010/09/16/1828502.html

正好做一個下載預覽功能,要開啟檔案,看到這篇相當不錯的文章就轉過來了。其中openFile方法可以自己改一下通過Intent.createChooser()方式來選擇開啟程式。

背景介紹:

MIME:全稱Multipurpose Internet Mail Extensions,多功能Internet 郵件擴充服務。它是一種多用途網際郵件擴充協議,在1992年最早應用於電子郵件系統,但後來也應用到瀏覽器。MIME類型就是設定某種副檔名的檔案用一 種應用程式來開啟的方式類型,當該副檔名檔案被訪問的時候,瀏覽器會自動使用指定應用程式來開啟。多用於指定一些用戶端自訂的檔案名稱,以及一些媒體檔案 開啟檔案。

在Android中通過檔案的MIME類型來判斷有哪些應用程式可以處理這些檔案,並使用其中的某一個應用程式(如果有多個可選的應用程式,則使用者必須指定一個)處理之。

我在寫android資源管理員(檔案瀏覽器)的時候,希望能在資源管理員的中實現開啟檔案的操作,此時就需要用到檔案的MIME類型。

---------------------------------------- 背景分割線 -----------------------------------------

實現方法:

 

/** * 根據檔案尾碼名獲得對應的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();    if(end=="")return type;    //在MIME和檔案類型的匹配表中找到對應的MIME類型。    for(int i=0;i<MIME_MapTable.length;i++){        if(end.equals(MIME_MapTable[i][0]))            type = MIME_MapTable[i][1];    }    return type;}/** * 開啟檔案 * @param file */private void openFile(File file){    //Uri uri = Uri.parse("file://"+file.getAbsolutePath());    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);    //跳轉    startActivity(intent);    }

 

 現在就差一個MIME類型和檔案類型的匹配表了。

 

"檔案類型——MIME類型"的匹配表:

 

//建立一個MIME類型與檔案尾碼名的匹配表private 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"},    {".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/xml"},    {".xml",    "text/plain"},    {".z",        "application/x-compress"},    {".zip",    "application/zip"},    {"",        "*/*"}    };

 

 

 

 

Android開啟不同類型檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.