当前位置:   article > 正文

Android 下载文件后,调用系统文件管理器打开方式_android用系统自带的打开方式打开文件

android用系统自带的打开方式打开文件

如果是定制的系统,可能需要注意下有没有内置播放器或者浏览软件!!! 

看效果:

第一先上个文件类型判断的方法:

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

第二直接传入文件路径

  1. /**
  2. * 根据路径打开文件
  3. *
  4. * @param path 文件路径
  5. */
  6. private void openAssignFolder(String path) {
  7. File file = new File(path);
  8. if (null == file || !file.exists()) {
  9. return;
  10. }
  11. //文件的类型
  12. String type = "";
  13. for (int i = 0; i < MATCH_ARRAY.length; i++) {
  14. //判断文件的格式
  15. if (path.toString().contains(MATCH_ARRAY[i][0].toString())) {
  16. type = MATCH_ARRAY[i][1];
  17. break;
  18. }
  19. }
  20. Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  21. intent.addCategory(Intent.CATEGORY_DEFAULT);
  22. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  23. intent.setAction(Intent.ACTION_VIEW);
  24. intent.setDataAndType(Uri.fromFile(file), type);
  25. try {
  26. //startActivity(intent);
  27. startActivity(Intent.createChooser(intent, "选择浏览工具"));
  28. } catch (ActivityNotFoundException e) {
  29. e.printStackTrace();
  30. }
  31. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/669264
推荐阅读
相关标签
  

闽ICP备14008679号