Android中调用系统所装的软件打开文件(转)

在应用中如何调用系统所装的软件打开一个文件,这是我们经常碰到的问题,下面是我所用到的一种方法,和大家一起分享一下!

这个是打开文件的一个方法:

  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类型可能不够完整,你有要补充的吗?

原文:http://tonysun3544.iteye.com/blog/1265884

Android中调用系统所装的软件打开文件(转)的更多相关文章

  1. Java乔晓松-android中调用系统拍照功能并显示拍照的图片

    android中调用系统拍照功能并显示拍照的图片 如果你是拍照完,利用onActivityResult获取data数据,把data数据转换成Bitmap数据,这样获取到的图片,是拍照的照片的缩略图 代 ...

  2. android中调用系统的发送短信、发送邮件、打电话功能

    1 调用发送短信功能: Uri smsToUri = Uri.parse("smsto:");  Intent sendIntent = new Intent(Intent.ACT ...

  3. Android中调用系统的相机和图库获取图片

    //--------我的主布局文件------很简单---------------------------------<LinearLayout xmlns:android="http ...

  4. 关于android中调用系统拍照,返回图片是旋转90度

    转载博客:http://blog.csdn.net/walker02/article/details/8211628 项目开发中遇到的一个问题,对于三星手机在做手机照片选择时出现图片显示不正常,研究后 ...

  5. 【转】Android 学习笔记——利用JNI技术在Android中调用、调试C++代码

    原文网址:http://cherishlc.iteye.com/blog/1756762 在Android中调用C++其实就是在Java中调用C++代码,只是在windows下编译生成DLL,在And ...

  6. Android中消息系统模型和Handler Looper

    http://www.cnblogs.com/bastard/archive/2012/06/08/2541944.html Android中消息系统模型和Handler Looper 作为Andro ...

  7. [转][android][利用JNI技术在Android中调用、调试C++代码]

    在Android中调用C++其实就是在Java中调用C++代码,只是在windows下编译生成DLL,在Android中会生成Linux系统下的.so文件(好吧,其实我基本没用过Linux). 没写过 ...

  8. Android 调用系统分享文字、图片、文件,可直达微信、朋友圈、QQ、QQ空间、微博

    原文:Android 调用系统分享文字.图片.文件,可直达微信.朋友圈.QQ.QQ空间.微博 兼容SDK 18以上的系统,直接调用系统分享功能,分享文本.图片.文件到第三方APP,如:微信.QQ.微博 ...

  9. 在Android中调用WebService

    某些情况下我们可能需要与Mysql或者Oracle数据库进行数据交互,有些朋友的第一反应就是直接在Android中加载驱动然后进行数据的增删改查.我个人不推荐这种做法,一是手机毕竟不是电脑,操作大量数 ...

随机推荐

  1. CSU-1989 赶路的小X

    题目链接 http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=1989 题目 Description A国一共有N座城市,由M条双向公路连 ...

  2. JS正则表达式 简单应用

    知识点: 先生成一个正则规则的对象,使用test()对传入的字符串进行验证,返回布尔类型 代码: <!doctype html><html><head> <m ...

  3. 第八篇:python基础_8 面向对象与网络编程

    本篇内容 接口与归一化设计 多态与多态性 封装 面向对象高级 异常处理 网络编程 一. 接口与归一化设计 1.定义 (1)归一化让使用者无需关心对象的类是什么,只需要知道这些对象都具备某些功能就可以了 ...

  4. vue-router中scrollBehavior的巧妙用法

    问题:使用keep-alive标签后部分安卓机返回缓存页位置不精确问题 解决方案: <div id="app"> <keep-alive> <rout ...

  5. 用iFrame模拟Ajax上传文件

    前段时间在解决ajax上传文件时折腾了好一阵.直接用$.post上传文本信息肯定是没有问题的.但是$.post直接上传图片是不可行的. 后来看到网上的一些解决方案,有现成的ajax上传文件的封装的方法 ...

  6. Codeforces Round #355 (Div. 2) C 预处理

    C. Vanya and Label time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  7. bzoj1009 [HNOI2008] GT考试 矩阵乘法+dp+kmp

    1009: [HNOI2008]GT考试 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 4542  Solved: 2815[Submit][Statu ...

  8. 在Linux内核中添加系统调用,并编译内核

    1 环境准备 运行系统:vmware下安装的ubuntu10.10 32bit桌面版. 编译内核版本: linux-2.6.32.63 内核目录: /home/wanchouchou/linuxKer ...

  9. vue-cli脚手架的.babelrc文件

    虽然es6还没被浏览器全部支持,但是使用es6是大势所趋,所以babel应运而生将es6代码转换成浏览器能够识别的代码 什么是.babelrc文件呢? 熟悉linux的同学一定知道,rc结尾的文件通常 ...

  10. Angular(三)

    一.使用Module(模块)组织依赖关系 模块提供了一种方法,可以用来组织应用中一块功能区域的依赖关系:同时还提供了一种机制,可以自动解析依赖关系(又叫做依赖注入).一般来说,我们把这些叫做依赖服务, ...