android调用第三方软件打开下载的附件

原创
2017/08/27 14:25
阅读数 355

在做我们产品的时候,需要下载附件, 同时下载附件需要打开, 通过查阅一些资料发现,android对这支持做的非常好,通过anction ,加minitype ,加数据源,就能找到合适的软件进行打开你的下载的附件,下面是我整理的一个的java类,希望对你有帮助![html] view plain copy

  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++){   


[html] view plain copy

  1.         if(end.equals(MIME_MapTable[i][0]))   
  2.             type = MIME_MapTable[i][1];   
  3.     }          
  4.     return type;   
  5. }   


[html] view plain copy

  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. }  

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部