Android中调用系统所装的软件打开文件,android应用开发基础学堂在线答案

  1. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

  2. //设置intent的Action属性

  3. intent.setAction(Intent.ACTION_VIEW);

  4. //获取文件file的MIME类型

  5. String type = getMIMEType(file);

  6. //设置intent的data和Type属性。

  7. intent.setDataAndType(/*uri*/Uri.fromFile(file), type);

  8. //跳转

  9. startActivity(intent);

  10. }

  11. /**

  12. * 根据文件后缀名获得对应的MIME类型。

  13. * @param file

  14. */

  15. private String getMIMEType(File file) {

  16. String type="*/*";

  17. String fName = file.getName();

  18. //获取后缀名前

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

的分隔符"."在fName中的位置。

  1. int dotIndex = fName.lastIndexOf(".");

  2. if(dotIndex < 0){

  3. return type;

  4. }

  5. /* 获取文件的后缀名 */

  6. String end=fName.substring(dotIndex,fName.length()).toLowerCase();

  7. if(end=="")return type;

  8. //在MIME和文件类型的匹配表中找到对应的MIME类型。

  9. for(int i=0;i<MIME_MapTable.length;i++){ //MIME_MapTable??在这里你一定有疑问,这个MIME_MapTable是什么?

  10. if(end.equals(MIME_MapTable[i][0]))

  11. type = MIME_MapTable[i][1];

  12. }

  13. return type;

  14. }

MIME_MapTable是所有文件的后缀名所对应的MIME类型的一个String数组:

Java代码   收藏代码

  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”},

{".mpc",    “application/vnd.mpohun.certificate”},

  1. {".mpe",    “video/mpeg”},

  2. {".mpeg",   “video/mpeg”},

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值