歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中調用系統所裝的軟件打開文件

Android中調用系統所裝的軟件打開文件

日期:2017/3/1 10:39:00   编辑:Linux編程

在應用中如何調用系統所裝的軟件打開一個文件,這是我們經常碰到的問題,下面是我所用到的一種方法,和大家一起分享一下!

這個是打開文件的一個方法:

  1. /**
  2. * 打開文件
  3. * @param file
  4. */
  5. private void openFile(File file){
  6. Intent intent = new Intent();
  7. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  8. //設置intent的Action屬性
  9. intent.setAction(Intent.ACTION_VIEW);
  10. //獲取文件file的MIME類型
  11. String type = getMIMEType(file);
  12. //設置intent的data和Type屬性。
  13. intent.setDataAndType(/*uri*/Uri.fromFile(file), type);
  14. //跳轉
  15. startActivity(intent);
  16. }
  17. /**
  18. * 根據文件後綴名獲得對應的MIME類型。
  19. * @param file
  20. */
  21. private String getMIMEType(File file) {
  22. String type="*/*";
  23. String fName = file.getName();
  24. //獲取後綴名前的分隔符"."在fName中的位置。
  25. int dotIndex = fName.lastIndexOf(".");
  26. if(dotIndex < 0){
  27. return type;
  28. }
  29. /* 獲取文件的後綴名 */
  30. String end=fName.substring(dotIndex,fName.length()).toLowerCase();
  31. if(end=="")return type;
  32. //在MIME和文件類型的匹配表中找到對應的MIME類型。
  33. for(int i=0;i<MIME_MapTable.length;i++){ //MIME_MapTable??在這裡你一定有疑問,這個MIME_MapTable是什麼?
  34. if(end.equals(MIME_MapTable[i][0]))
  35. type = MIME_MapTable[i][1];
  36. }
  37. return type;
  38. }

MIME_MapTable是所有文件的後綴名所對應的MIME類型的一個String數組:

  1. private final String[][] MIME_MapTable={
  2. //{後綴名, MIME類型}
  3. {".3gp", "video/3gpp"},
  4. {".apk", "application/vnd.Android.package-archive"},
  5. {".asf", "video/x-ms-asf"},
  6. {".avi", "video/x-msvideo"},
  7. {".bin", "application/octet-stream"},
  8. {".bmp", "image/bmp"},
  9. {".c", "text/plain"},
  10. {".class", "application/octet-stream"},
  11. {".conf", "text/plain"},
  12. {".cpp", "text/plain"},
  13. {".doc", "application/msword"},
  14. {".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
  15. {".xls", "application/vnd.ms-excel"},
  16. {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
  17. {".exe", "application/octet-stream"},
  18. {".gif", "image/gif"},
  19. {".gtar", "application/x-gtar"},
  20. {".gz", "application/x-gzip"},
  21. {".h", "text/plain"},
  22. {".htm", "text/html"},
  23. {".html", "text/html"},
  24. {".jar", "application/java-archive"},
  25. {".java", "text/plain"},
  26. {".jpeg", "image/jpeg"},
  27. {".jpg", "image/jpeg"},
  28. {".js", "application/x-javascript"},
  29. {".log", "text/plain"},
  30. {".m3u", "audio/x-mpegurl"},
  31. {".m4a", "audio/mp4a-latm"},
  32. {".m4b", "audio/mp4a-latm"},
  33. {".m4p", "audio/mp4a-latm"},
  34. {".m4u", "video/vnd.mpegurl"},
  35. {".m4v", "video/x-m4v"},
  36. {".mov", "video/quicktime"},
  37. {".mp2", "audio/x-mpeg"},
  38. {".mp3", "audio/x-mpeg"},
  39. {".mp4", "video/mp4"},
  40. {".mpc", "application/vnd.mpohun.certificate"},
  41. {".mpe", "video/mpeg"},
  42. {".mpeg", "video/mpeg"},
  43. {".mpg", "video/mpeg"},
  44. {".mpg4", "video/mp4"},
  45. {".mpga", "audio/mpeg"},
  46. {".msg", "application/vnd.ms-outlook"},
  47. {".ogg", "audio/ogg"},
  48. {".pdf", "application/pdf"},
  49. {".png", "image/png"},
  50. {".pps", "application/vnd.ms-powerpoint"},
  51. {".ppt", "application/vnd.ms-powerpoint"},
  52. {".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
  53. {".prop", "text/plain"},
  54. {".rc", "text/plain"},
  55. {".rmvb", "audio/x-pn-realaudio"},
  56. {".rtf", "application/rtf"},
  57. {".sh", "text/plain"},
  58. {".tar", "application/x-tar"},
  59. {".tgz", "application/x-compressed"},
  60. {".txt", "text/plain"},
  61. {".wav", "audio/x-wav"},
  62. {".wma", "audio/x-ms-wma"},
  63. {".wmv", "audio/x-ms-wmv"},
  64. {".wps", "application/vnd.ms-works"},
  65. {".xml", "text/plain"},
  66. {".z", "application/x-compress"},
  67. {".zip", "application/x-zip-compressed"},
  68. {"", "*/*"}
  69. };

這個MIME類型可能不夠完整,你有要補充的嗎?

Copyright © Linux教程網 All Rights Reserved