歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android調用第三方軟件打開下載的附件

Android調用第三方軟件打開下載的附件

日期:2017/3/1 10:22:42   编辑:Linux編程

在做我們產品的時候,需要下載附件, 同時下載附件需要打開, 通過查閱一些資料發現,Android對這支持做的非常好,通過anction ,加minitype ,加數據源,就能找到合適的軟件進行打開你的下載的附件,下面是我整理的一個的java類,希望對你有幫助!

  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. import java.io.File;
  11. import android.content.ActivityNotFoundException;
  12. import android.content.Context;
  13. import android.content.Intent;
  14. import android.net.Uri;
  15. import android.widget.Toast;
  16. public class CallOtherOpeanFile {
  17. /**
  18. *
  19. * <code>openFile</code>
  20. * @description: TODO(打開附件)
  21. * @param context
  22. * @param file
  23. * @since 2012-5-19 liaoyp
  24. */
  25. public void openFile(Context context,File file){
  26. try{
  27. Intent intent = new Intent();
  28. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  29. //設置intent的Action屬性
  30. intent.setAction(Intent.ACTION_VIEW);
  31. //獲取文件file的MIME類型
  32. String type = getMIMEType(file);
  33. //設置intent的data和Type屬性。
  34. intent.setDataAndType(/*uri*/Uri.fromFile(file), type);
  35. //跳轉
  36. context.startActivity(intent);
  37. // Intent.createChooser(intent, "請選擇對應的軟件打開該附件!");
  38. }catch (ActivityNotFoundException e) {
  39. // TODO: handle exception
  40. Toast.makeText(context, "sorry附件不能打開,請下載相關軟件!", 500).show();
  41. }
  42. }
  43. private String getMIMEType(File file) {
  44. String type="*/*";
  45. String fName = file.getName();
  46. //獲取後綴名前的分隔符"."在fName中的位置。
  47. int dotIndex = fName.lastIndexOf(".");
  48. if(dotIndex < 0){
  49. return type;
  50. }
  51. /* 獲取文件的後綴名*/
  52. String end=fName.substring(dotIndex,fName.length()).toLowerCase();
  53. if(end=="")return type;
  54. //在MIME和文件類型的匹配表中找到對應的MIME類型。
  55. 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. }

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved