Android 開啟本地檔案

來源:互聯網
上載者:User

標籤:apt   atm   string   new   indexof   class   start   常見   html   

Android 開啟本地的檔案,目前來說,其實很常見。而且現在有手機版的office了。查看office的全家桶就更加方便。

首先要知道的是,Android 開啟本地檔案是根據類型開啟的,也就是根據檔案的 MIME 類型來確定

如果不知道是什麼類型,那就是 : */*

類型匹配表:

private 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"},            {".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"},            {"",        "*/*"}    };

  擷取檔案的類型。

    private String getMIMEType(File file) {        String type="*/*";        String fName = file.getName();        //擷取尾碼名前的分隔字元"."在fName中的位置。          int dotIndex = fName.lastIndexOf(".");        if(dotIndex < 0)            return type;        /* 擷取檔案的尾碼名 */        String fileType = fName.substring(dotIndex,fName.length()).toLowerCase();        if(fileType == null || "".equals(fileType))            return type;        //在MIME和檔案類型的匹配表中找到對應的MIME類型。          for(int i=0;i<MIME_MapTable.length;i++){              if(fileType.equals(MIME_MapTable[i][0]))                type = MIME_MapTable[i][1];        }        return type;    }

  開啟檔案

    Intent intent = new Intent();    File file = new File(FileDirectory);    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//設定標記    intent.setAction(Intent.ACTION_VIEW);//動作,查看    intent.setDataAndType(Uri.fromFile(file), getMIMEType(file));//設定類型    context.startActivity(intent);

  

 

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.