android 打开各种文件(setDataAndType)

Java代码  /**
 * 打开文件
 * @param file
 */ 
private 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); //    public Intent setDataAndType(Uri data, String type)
    //跳转 
    startActivity(intent);     //这里最好try一下,有可能会报错。 //比如说你的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++){ //MIME_MapTable??在这里你一定有疑问,这个MIME_MapTable是什么? 
        if(end.equals(MIME_MapTable[i][0])) 
            type = MIME_MapTable[i][1]; 
    }        
    return type; 

 

====================== 常用 type =========================

一、music:

String file_type = "audio/*";


二、movice:

String file_type = "video/*";


三、pdf:

String file_type = "application/*";


四、picture:

String file_type = "image/*";



================================================

  1. ".*"="application/octet-stream"  
  2. ".001"="application/x-001"  
  3. ".301"="application/x-301"  
  4. ".323"="text/h323"  
  5. ".906"="application/x-906"  
  6. ".907"="drawing/907"  
  7. ".a11"="application/x-a11"  
  8. ".acp"="audio/x-mei-aac"  
  9. ".ai"="application/postscript"  
  10. ".aif"="audio/aiff"  
  11. ".aifc"="audio/aiff"  
  12. ".aiff"="audio/aiff"  
  13. ".anv"="application/x-anv"  
  14. ".asa"="text/asa"  
  15. ".asf"="video/x-ms-asf"  
  16. ".asp"="text/asp"  
  17. ".asx"="video/x-ms-asf"  
  18. ".au"="audio/basic"  
  19. ".avi"="video/avi"  
  20. ".awf"="application/vnd.adobe.workflow"  
  21. ".biz"="text/xml"  
  22. ".bmp"="application/x-bmp"  
  23. ".bot"="application/x-bot"  
  24. ".c4t"="application/x-c4t"  
  25. ".c90"="application/x-c90"  
  26. ".cal"="application/x-cals"  
  27. ".cat"="application/vnd.ms-pki.seccat"  
  28. ".cdf"="application/x-netcdf"  
  29. ".cdr"="application/x-cdr"  
  30. ".cel"="application/x-cel"  
  31. ".cer"="application/x-x509-ca-cert"  
  32. ".cg4"="application/x-g4"  
  33. ".cgm"="application/x-cgm"  
  34. ".cit"="application/x-cit"  
  35. ".class"="java/*"  
  36. ".cml"="text/xml"  
  37. ".cmp"="application/x-cmp"  
  38. ".cmx"="application/x-cmx"  
  39. ".cot"="application/x-cot"  
  40. ".crl"="application/pkix-crl"  
  41. ".crt"="application/x-x509-ca-cert"  
  42. ".csi"="application/x-csi"  
  43. ".css"="text/css"  
  44. ".cut"="application/x-cut"  
  45. ".dbf"="application/x-dbf"  
  46. ".dbm"="application/x-dbm"  
  47. ".dbx"="application/x-dbx"  
  48. ".dcd"="text/xml"  
  49. ".dcx"="application/x-dcx"  
  50. ".der"="application/x-x509-ca-cert"  
  51. ".dgn"="application/x-dgn"  
  52. ".dib"="application/x-dib"  
  53. ".dll"="application/x-msdownload"  
  54. ".doc"="application/msword"  
  55. ".dot"="application/msword"  
  56. ".drw"="application/x-drw"  
  57. ".dtd"="text/xml"  
  58. ".dwf"="Model/vnd.dwf"  
  59. ".dwf"="application/x-dwf"  
  60. ".dwg"="application/x-dwg"  
  61. ".dxb"="application/x-dxb"  
  62. ".dxf"="application/x-dxf"  
  63. ".edn"="application/vnd.adobe.edn"  
  64. ".emf"="application/x-emf"  
  65. ".eml"="message/rfc822"  
  66. ".ent"="text/xml"  
  67. ".epi"="application/x-epi"  
  68. ".eps"="application/x-ps"  
  69. ".eps"="application/postscript"  
  70. ".etd"="application/x-ebx"  
  71. ".exe"="application/x-msdownload"  
  72. ".fax"="image/fax"  
  73. ".fdf"="application/vnd.fdf"  
  74. ".fif"="application/fractals"  
  75. ".fo"="text/xml"  
  76. ".frm"="application/x-frm"  
  77. ".g4"="application/x-g4"  
  78. ".gbr"="application/x-gbr"  
  79. ".gcd"="application/x-gcd"  
  80. ".gif"="image/gif"  
  81. ".gl2"="application/x-gl2"  
  82. ".gp4"="application/x-gp4"  
  83. ".hgl"="application/x-hgl"  
  84. ".hmr"="application/x-hmr"  
  85. ".hpg"="application/x-hpgl"  
  86. ".hpl"="application/x-hpl"  
  87. ".hqx"="application/mac-binhex40"  
  88. ".hrf"="application/x-hrf"  
  89. ".hta"="application/hta"  
  90. ".htc"="text/x-component"  
  91. ".htm"="text/html"  
  92. ".html"="text/html"  
  93. ".htt"="text/webviewhtml"  
  94. ".htx"="text/html"  
  95. ".icb"="application/x-icb"  
  96. ".ico"="image/x-icon"  
  97. ".ico"="application/x-ico"  
  98. ".iff"="application/x-iff"  
  99. ".ig4"="application/x-g4"  
  100. ".igs"="application/x-igs"  
  101. ".iii"="application/x-iphone"  
  102. ".img"="application/x-img"  
  103. ".ins"="application/x-internet-signup"  
  104. ".isp"="application/x-internet-signup"  
  105. ".IVF"="video/x-ivf"  
  106. ".java"="java/*"  
  107. ".jfif"="image/jpeg"  
  108. ".jpe"="image/jpeg"  
  109. ".jpe"="application/x-jpe"  
  110. ".jpeg"="image/jpeg"  
  111. ".jpg"="image/jpeg"  
  112. ".jpg"="application/x-jpg"  
  113. ".js"="application/x-javascript"  
  114. ".jsp"="text/html"  
  115. ".la1"="audio/x-liquid-file"  
  116. ".lar"="application/x-laplayer-reg"  
  117. ".latex"="application/x-latex"  
  118. ".lavs"="audio/x-liquid-secure"  
  119. ".lbm"="application/x-lbm"  
  120. ".lmsff"="audio/x-la-lms"  
  121. ".ls"="application/x-javascript"  
  122. ".ltr"="application/x-ltr"  
  123. ".m1v"="video/x-mpeg"  
  124. ".m2v"="video/x-mpeg"  
  125. ".m3u"="audio/mpegurl"  
  126. ".m4e"="video/mpeg4"  
  127. ".mac"="application/x-mac"  
  128. ".man"="application/x-troff-man"  
  129. ".math"="text/xml"  
  130. ".mdb"="application/msaccess"  
  131. ".mdb"="application/x-mdb"  
  132. ".mfp"="application/x-shockwave-flash"  
  133. ".mht"="message/rfc822"  
  134. ".mhtml"="message/rfc822"  
  135. ".mi"="application/x-mi"  
  136. ".mid"="audio/mid"  
  137. ".midi"="audio/mid"  
  138. ".mil"="application/x-mil"  
  139. ".mml"="text/xml"  
  140. ".mnd"="audio/x-musicnet-download"  
  141. ".mns"="audio/x-musicnet-stream"  
  142. ".mocha"="application/x-javascript"  
  143. ".movie"="video/x-sgi-movie"  
  144. ".mp1"="audio/mp1"  
  145. ".mp2"="audio/mp2"  
  146. ".mp2v"="video/mpeg"  
  147. ".mp3"="audio/mp3"  
  148. ".mp4"="video/mpeg4"  
  149. ".mpa"="video/x-mpg"  
  150. ".mpd"="application/vnd.ms-project"  
  151. ".mpe"="video/x-mpeg"  
  152. ".mpeg"="video/mpg"  
  153. ".mpg"="video/mpg"  
  154. ".mpga"="audio/rn-mpeg"  
  155. ".mpp"="application/vnd.ms-project"  
  156. ".mps"="video/x-mpeg"  
  157. ".mpt"="application/vnd.ms-project"  
  158. ".mpv"="video/mpg"  
  159. ".mpv2"="video/mpeg"  
  160. ".mpw"="application/vnd.ms-project"  
  161. ".mpx"="application/vnd.ms-project"  
  162. ".mtx"="text/xml"  
  163. ".mxp"="application/x-mmxp"  
  164. ".net"="image/pnetvue"  
  165. ".nrf"="application/x-nrf"  
  166. ".nws"="message/rfc822"  
  167. ".odc"="text/x-ms-odc"  
  168. ".out"="application/x-out"  
  169. ".p10"="application/pkcs10"  
  170. ".p12"="application/x-pkcs12"  
  171. ".p7b"="application/x-pkcs7-certificates"  
  172. ".p7c"="application/pkcs7-mime"  
  173. ".p7m"="application/pkcs7-mime"  
  174. ".p7r"="application/x-pkcs7-certreqresp"  
  175. ".p7s"="application/pkcs7-signature"  
  176. ".pc5"="application/x-pc5"  
  177. ".pci"="application/x-pci"  
  178. ".pcl"="application/x-pcl"  
  179. ".pcx"="application/x-pcx"  
  180. ".pdf"="application/pdf"  
  181. ".pdf"="application/pdf"  
  182. ".pdx"="application/vnd.adobe.pdx"  
  183. ".pfx"="application/x-pkcs12"  
  184. ".pgl"="application/x-pgl"  
  185. ".pic"="application/x-pic"  
  186. ".pko"="application/vnd.ms-pki.pko"  
  187. ".pl"="application/x-perl"  
  188. ".plg"="text/html"  
  189. ".pls"="audio/scpls"  
  190. ".plt"="application/x-plt"  
  191. ".png"="image/png"  
  192. ".png"="application/x-png"  
  193. ".pot"="application/vnd.ms-powerpoint"  
  194. ".ppa"="application/vnd.ms-powerpoint"  
  195. ".ppm"="application/x-ppm"  
  196. ".pps"="application/vnd.ms-powerpoint"  
  197. ".ppt"="application/vnd.ms-powerpoint"  
  198. ".ppt"="application/x-ppt"  
  199. ".pr"="application/x-pr"  
  200. ".prf"="application/pics-rules"  
  201. ".prn"="application/x-prn"  
  202. ".prt"="application/x-prt"  
  203. ".ps"="application/x-ps"  
  204. ".ps"="application/postscript"  
  205. ".ptn"="application/x-ptn"  
  206. ".pwz"="application/vnd.ms-powerpoint"  
  207. ".r3t"="text/vnd.rn-realtext3d"  
  208. ".ra"="audio/vnd.rn-realaudio"  
  209. ".ram"="audio/x-pn-realaudio"  
  210. ".ras"="application/x-ras"  
  211. ".rat"="application/rat-file"  
  212. ".rdf"="text/xml"  
  213. ".rec"="application/vnd.rn-recording"  
  214. ".red"="application/x-red"  
  215. ".rgb"="application/x-rgb"  
  216. ".rjs"="application/vnd.rn-realsystem-rjs"  
  217. ".rjt"="application/vnd.rn-realsystem-rjt"  
  218. ".rlc"="application/x-rlc"  
  219. ".rle"="application/x-rle"  
  220. ".rm"="application/vnd.rn-realmedia"  
  221. ".rmf"="application/vnd.adobe.rmf"  
  222. ".rmi"="audio/mid"  
  223. ".rmj"="application/vnd.rn-realsystem-rmj"  
  224. ".rmm"="audio/x-pn-realaudio"  
  225. ".rmp"="application/vnd.rn-rn_music_package"  
  226. ".rms"="application/vnd.rn-realmedia-secure"  
  227. ".rmvb"="application/vnd.rn-realmedia-vbr"  
  228. ".rmx"="application/vnd.rn-realsystem-rmx"  
  229. ".rnx"="application/vnd.rn-realplayer"  
  230. ".rp"="image/vnd.rn-realpix"  
  231. ".rpm"="audio/x-pn-realaudio-plugin"  
  232. ".rsml"="application/vnd.rn-rsml"  
  233. ".rt"="text/vnd.rn-realtext"  
  234. ".rtf"="application/msword"  
  235. ".rtf"="application/x-rtf"  
  236. ".rv"="video/vnd.rn-realvideo"  
  237. ".sam"="application/x-sam"  
  238. ".sat"="application/x-sat"  
  239. ".sdp"="application/sdp"  
  240. ".sdw"="application/x-sdw"  
  241. ".sit"="application/x-stuffit"  
  242. ".slb"="application/x-slb"  
  243. ".sld"="application/x-sld"  
  244. ".slk"="drawing/x-slk"  
  245. ".smi"="application/smil"  
  246. ".smil"="application/smil"  
  247. ".smk"="application/x-smk"  
  248. ".snd"="audio/basic"  
  249. ".sol"="text/plain"  
  250. ".sor"="text/plain"  
  251. ".spc"="application/x-pkcs7-certificates"  
  252. ".spl"="application/futuresplash"  
  253. ".spp"="text/xml"  
  254. ".ssm"="application/streamingmedia"  
  255. ".sst"="application/vnd.ms-pki.certstore"  
  256. ".stl"="application/vnd.ms-pki.stl"  
  257. ".stm"="text/html"  
  258. ".sty"="application/x-sty"  
  259. ".svg"="text/xml"  
  260. ".swf"="application/x-shockwave-flash"  
  261. ".tdf"="application/x-tdf"  
  262. ".tg4"="application/x-tg4"  
  263. ".tga"="application/x-tga"  
  264. ".tif"="image/tiff"  
  265. ".tif"="application/x-tif"  
  266. ".tiff"="image/tiff"  
  267. ".tld"="text/xml"  
  268. ".top"="drawing/x-top"  
  269. ".torrent"="application/x-bittorrent"  
  270. ".tsd"="text/xml"  
  271. ".txt"="text/plain"  
  272. ".uin"="application/x-icq"  
  273. ".uls"="text/iuls"  
  274. ".vcf"="text/x-vcard"  
  275. ".vda"="application/x-vda"  
  276. ".vdx"="application/vnd.visio"  
  277. ".vml"="text/xml"  
  278. ".vpg"="application/x-vpeg005"  
  279. ".vsd"="application/vnd.visio"  
  280. ".vsd"="application/x-vsd"  
  281. ".vss"="application/vnd.visio"  
  282. ".vst"="application/vnd.visio"  
  283. ".vst"="application/x-vst"  
  284. ".vsw"="application/vnd.visio"  
  285. ".vsx"="application/vnd.visio"  
  286. ".vtx"="application/vnd.visio"  
  287. ".vxml"="text/xml"  
  288. ".wav"="audio/wav"  
  289. ".wax"="audio/x-ms-wax"  
  290. ".wb1"="application/x-wb1"  
  291. ".wb2"="application/x-wb2"  
  292. ".wb3"="application/x-wb3"  
  293. ".wbmp"="image/vnd.wap.wbmp"  
  294. ".wiz"="application/msword"  
  295. ".wk3"="application/x-wk3"  
  296. ".wk4"="application/x-wk4"  
  297. ".wkq"="application/x-wkq"  
  298. ".wks"="application/x-wks"  
  299. ".wm"="video/x-ms-wm"  
  300. ".wma"="audio/x-ms-wma"  
  301. ".wmd"="application/x-ms-wmd"  
  302. ".wmf"="application/x-wmf"  
  303. ".wml"="text/vnd.wap.wml"  
  304. ".wmv"="video/x-ms-wmv"  
  305. ".wmx"="video/x-ms-wmx"  
  306. ".wmz"="application/x-ms-wmz"  
  307. ".wp6"="application/x-wp6"  
  308. ".wpd"="application/x-wpd"  
  309. ".wpg"="application/x-wpg"  
  310. ".wpl"="application/vnd.ms-wpl"  
  311. ".wq1"="application/x-wq1"  
  312. ".wr1"="application/x-wr1"  
  313. ".wri"="application/x-wri"  
  314. ".wrk"="application/x-wrk"  
  315. ".ws"="application/x-ws"  
  316. ".ws2"="application/x-ws"  
  317. ".wsc"="text/scriptlet"  
  318. ".wsdl"="text/xml"  
  319. ".wvx"="video/x-ms-wvx"  
  320. ".xdp"="application/vnd.adobe.xdp"  
  321. ".xdr"="text/xml"  
  322. ".xfd"="application/vnd.adobe.xfd"  
  323. ".xfdf"="application/vnd.adobe.xfdf"  
  324. ".xhtml"="text/html"  
  325. ".xls"="application/vnd.ms-excel"  
  326. ".xls"="application/x-xls"  
  327. ".xlw"="application/x-xlw"  
  328. ".xml"="text/xml"  
  329. ".xpl"="audio/scpls"  
  330. ".xq"="text/xml"  
  331. ".xql"="text/xml"  
  332. ".xquery"="text/xml"  
  333. ".xsd"="text/xml"  
  334. ".xsl"="text/xml"  
  335. ".xslt"="text/xml"  
  336. ".xwd"="application/x-xwd"  
  337. ".x_b"="application/x-x_b"  
  338. ".x_t"="application/x-x_t" 

 

更多 参考: http://blog.csdn.net/tt5267621/article/details/7173972



posted @ 2014-11-04 11:53  点点爱梦  阅读(1137)  评论(0编辑  收藏  举报