- 1./*
- 2. * @project C6Client
- 3. * @package com.jh.c6.util
- 4. * @file CallOtherOpeanFile.java
- 5. * @version 1.0
- 6. * @author liaoyp
- 7. * @time 2012-5-19 上午6:30:54
- 8. * */
- 9.package com.jh.c6.util;
- 10.
- 11.import java.io.File;
- 12.
- 13.import Android.content.ActivityNotFoundException;
- 14.import Android.content.Context;
- 15.import Android.content.Intent;
- 16.import Android.net.Uri;
- 17.import Android.widget.Toast;
- 18.
- 19.public class CallOtherOpeanFile {
- 20. /**
- 21. *
- 22. * <code>openFile</code>
- 23. * @description: TODO(打开附件)
- 24. * @param context
- 25. * @param file
- 26. * @since 2012-5-19 liaoyp
- 27. */
- 28. public void openFile(Context context,File file){
- 29. try{
- 30. Intent intent = new Intent();
- 31. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- 32. //设置intent的Action属性
- 33. intent.setAction(Intent.ACTION_VIEW);
- 34. //获取文件file的MIME类型
- 35. String type = getMIMEType(file);
- 36. //设置intent的data和Type属性。
- 37. intent.setDataAndType(/*uri*/Uri.fromFile(file), type);
- 38. //跳转
- 39. context.startActivity(intent);
- 40.// Intent.createChooser(intent, "请选择对应的软件打开该附件!");
- 41. }catch (ActivityNotFoundException e) {
- 42. // TODO: handle exception
- 43. Toast.makeText(context, "sorry附件不能打开,请下载相关软件!", 500).show();
- 44. }
- 45. }
- 46. private String getMIMEType(File file) {
- 47.
- 48. String type="*/*";
- 49. String fName = file.getName();
- 50. //获取后缀名前的分隔符"."在fName中的位置。
- 51. int dotIndex = fName.lastIndexOf(".");
- 52. if(dotIndex < 0){
- 53. return type;
- 54. }
- 55. /* 获取文件的后缀名*/
- 56. String end=fName.substring(dotIndex,fName.length()).toLowerCase();
- 57. if(end=="")return type;
- 58. //在MIME和文件类型的匹配表中找到对应的MIME类型。
- 59. for(int i=0;i<MIME_MapTable.length;i++){
- 1. if(end.equals(MIME_MapTable[i][0]))
- 2. type = MIME_MapTable[i][1];
- 3. }
- 4. return type;
- 5.}
- 1.// 可以自己随意添加
- 2. private String [][] MIME_MapTable={
- 3. //{后缀名,MIME类型}
- 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. {".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
- 16. {".xls", "application/vnd.ms-excel"},
- 17. {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
- 18. {".exe", "application/octet-stream"},
- 19. {".gif", "image/gif"},
- 20. {".gtar", "application/x-gtar"},
- 21. {".gz", "application/x-gzip"},
- 22. {".h", "text/plain"},
- 23. {".htm", "text/html"},
- 24. {".html", "text/html"},
- 25. {".jar", "application/java-archive"},
- 26. {".java", "text/plain"},
- 27. {".jpeg", "image/jpeg"},
- 28. {".jpg", "image/jpeg"},
- 29. {".js", "application/x-javascript"},
- 30. {".log", "text/plain"},
- 31. {".m3u", "audio/x-mpegurl"},
- 32. {".m4a", "audio/mp4a-latm"},
- 33. {".m4b", "audio/mp4a-latm"},
- 34. {".m4p", "audio/mp4a-latm"},
- 35. {".m4u", "video/vnd.mpegurl"},
- 36. {".m4v", "video/x-m4v"},
- 37. {".mov", "video/quicktime"},
- 38. {".mp2", "audio/x-mpeg"},
- 39. {".mp3", "audio/x-mpeg"},
- 40. {".mp4", "video/mp4"},
- 41. {".mpc", "application/vnd.mpohun.certificate"},
- 42. {".mpe", "video/mpeg"},
- 43. {".mpeg", "video/mpeg"},
- 44. {".mpg", "video/mpeg"},
- 45. {".mpg4", "video/mp4"},
- 46. {".mpga", "audio/mpeg"},
- 47. {".msg", "application/vnd.ms-outlook"},
- 48. {".ogg", "audio/ogg"},
- 49. {".pdf", "application/pdf"},
- 50. {".png", "image/png"},
- 51. {".pps", "application/vnd.ms-powerpoint"},
- 52. {".ppt", "application/vnd.ms-powerpoint"},
- 53. {".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
- 54. {".prop", "text/plain"},
- 55. {".rc", "text/plain"},
- 56. {".rmvb", "audio/x-pn-realaudio"},
- 57. {".rtf", "application/rtf"},
- 58. {".sh", "text/plain"},
- 59. {".tar", "application/x-tar"},
- 60. {".tgz", "application/x-compressed"},
- 61. {".txt", "text/plain"},
- 62. {".wav", "audio/x-wav"},
- 63. {".wma", "audio/x-ms-wma"},
- 64. {".wmv", "audio/x-ms-wmv"},
- 65. {".wps", "application/vnd.ms-works"},
- 66. {".xml", "text/plain"},
- 67. {".z", "application/x-compress"},
- 68. {".zip", "application/x-zip-compressed"},
- 69. {"", "*/*"}
- 70. };
- 71.
- 72.
- 73.}
Android调用第三方软件打开下载的附件
猜你喜欢
转载自sunzeping.iteye.com/blog/1883695
今日推荐
周排行