Android调用系统应用打开各种类型文件

  1. //建立一个文件类型与文件后缀名的匹配表

  2. private 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. * @param context 上下文

  4. * @param path 文件路径

  5. */

  6. public static void openFileByPath(Context context,String path) {

  7. if(context==null||path==null)

  8. return;

  9. Intent intent = new Intent();

  10. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

  11. //设置intent的Action属性

  12. intent.setAction(Intent.ACTION_VIEW);

  13. //文件的类型

  14. String type = "";

  15. for(int i =0;i<MATCH_ARRAY.length;i++){

  16. //判断文件的格式

  17. if(path.toString().contains(MATCH_ARRAY[i][0].toString())){

  18. type = MATCH_ARRAY[i][1];

  19. break;

  20. }

  21. }

  22. try {

  23. //设置intent的data和Type属性

  24. intent.setDataAndType(Uri.fromFile(new File(path)), type);

  25. //跳转

  26. context.startActivity(intent);

  27. } catch (Exception e) { //当系统没有携带文件打开软件,提示

  28. ShowToastUtil.showToast(context,"无法打开该格式文件!");

  29. e.printStackTrace();

  30. }

  31. }

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值