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

 

http://whatandroid.blog.51cto.com/2172732/873765     

 

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

 

  1. 1./*   
  2.  2. * @project C6Client   
  3.  3. * @package com.jh.c6.util   
  4.  4. * @file CallOtherOpeanFile.java   
  5.  5. * @version  1.0   
  6.  6. * @author  liaoyp   
  7.  7. * @time  2012-5-19 上午6:30:54   
  8.  8. * */    
  9.  9.package com.jh.c6.util;    
  10.  10.    
  11.  11.import java.io.File;    
  12.  12.    
  13.  13.import Android.content.ActivityNotFoundException;    
  14.  14.import Android.content.Context;    
  15.  15.import Android.content.Intent;    
  16.  16.import Android.net.Uri;    
  17.  17.import Android.widget.Toast;    
  18.  18.    
  19.  19.public class CallOtherOpeanFile {    
  20.  20.    /**   
  21.  21.     *    
  22.  22.     * <code>openFile</code>   
  23.  23.     * @description: TODO(打开附件)    
  24.  24.     * @param context   
  25.  25.     * @param file   
  26.  26.     * @since   2012-5-19    liaoyp   
  27.  27.     */    
  28.  28.    public void openFile(Context context,File file){     
  29.  29.        try{    
  30.  30.        Intent intent = new Intent();     
  31.  31.        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);     
  32.  32.        //设置intent的Action属性      
  33.  33.        intent.setAction(Intent.ACTION_VIEW);     
  34.  34.        //获取文件file的MIME类型      
  35.  35.        String type = getMIMEType(file);     
  36.  36.        //设置intent的data和Type属性。      
  37.  37.        intent.setDataAndType(/*uri*/Uri.fromFile(file), type);     
  38.  38.        //跳转      
  39.  39.        context.startActivity(intent);       
  40.  40.//      Intent.createChooser(intent, "请选择对应的软件打开该附件!");     
  41.  41.        }catch (ActivityNotFoundException e) {    
  42.  42.            // TODO: handle exception     
  43.  43.            Toast.makeText(context, "sorry附件不能打开,请下载相关软件!"500).show();    
  44.  44.        }    
  45.  45.    }     
  46.  46.    private String getMIMEType(File file) {     
  47.  47.             
  48.  48.        String type="*/*";     
  49.  49.        String fName = file.getName();     
  50.  50.        //获取后缀名前的分隔符"."在fName中的位置。      
  51.  51.        int dotIndex = fName.lastIndexOf(".");     
  52.  52.        if(dotIndex < 0){     
  53.  53.            return type;     
  54.  54.        }     
  55.  55.        /* 获取文件的后缀名*/     
  56.  56.        String end=fName.substring(dotIndex,fName.length()).toLowerCase();     
  57.  57.        if(end=="")return type;     
  58.  58.        //在MIME和文件类型的匹配表中找到对应的MIME类型。      
  59.  59.        for(int i=0;i<MIME_MapTable.length;i++){     
  60.  
  61.    
  62.  
  63.    
  64.  1.        if(end.equals(MIME_MapTable[i][0]))     
  65.  2.            type = MIME_MapTable[i][1];     
  66.  3.    }            
  67.  4.    return type;     
  68.  5.}     
  69.  
  70.    
  71.    
  72.    
  73. 1.// 可以自己随意添加     
  74.  2.     private String [][]  MIME_MapTable={     
  75.  3.            //{后缀名,MIME类型}      
  76.  4.            {".3gp",    "video/3gpp"},     
  77.  5.            {".apk",    "application/vnd.Android.package-archive"},     
  78.  6.            {".asf",    "video/x-ms-asf"},     
  79.  7.            {".avi",    "video/x-msvideo"},     
  80.  8.            {".bin",    "application/octet-stream"},     
  81.  9.            {".bmp",    "image/bmp"},     
  82.  10.            {".c",  "text/plain"},     
  83.  11.            {".class",  "application/octet-stream"},     
  84.  12.            {".conf",   "text/plain"},     
  85.  13.            {".cpp",    "text/plain"},     
  86.  14.            {".doc",    "application/msword"},     
  87.  15.            {".docx",   "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},     
  88.  16.            {".xls",    "application/vnd.ms-excel"},      
  89.  17.            {".xlsx",   "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},     
  90.  18.            {".exe",    "application/octet-stream"},     
  91.  19.            {".gif",    "image/gif"},     
  92.  20.            {".gtar",   "application/x-gtar"},     
  93.  21.            {".gz""application/x-gzip"},     
  94.  22.            {".h",  "text/plain"},     
  95.  23.            {".htm",    "text/html"},     
  96.  24.            {".html",   "text/html"},     
  97.  25.            {".jar",    "application/java-archive"},     
  98.  26.            {".java",   "text/plain"},     
  99.  27.            {".jpeg",   "image/jpeg"},     
  100.  28.            {".jpg",    "image/jpeg"},     
  101.  29.            {".js""application/x-javascript"},     
  102.  30.            {".log",    "text/plain"},     
  103.  31.            {".m3u",    "audio/x-mpegurl"},     
  104.  32.            {".m4a",    "audio/mp4a-latm"},     
  105.  33.            {".m4b",    "audio/mp4a-latm"},     
  106.  34.            {".m4p",    "audio/mp4a-latm"},     
  107.  35.            {".m4u",    "video/vnd.mpegurl"},     
  108.  36.            {".m4v",    "video/x-m4v"},      
  109.  37.            {".mov",    "video/quicktime"},     
  110.  38.            {".mp2",    "audio/x-mpeg"},     
  111.  39.            {".mp3",    "audio/x-mpeg"},     
  112.  40.            {".mp4",    "video/mp4"},     
  113.  41.            {".mpc",    "application/vnd.mpohun.certificate"},            
  114.  42.            {".mpe",    "video/mpeg"},       
  115.  43.            {".mpeg",   "video/mpeg"},       
  116.  44.            {".mpg",    "video/mpeg"},       
  117.  45.            {".mpg4",   "video/mp4"},        
  118.  46.            {".mpga",   "audio/mpeg"},     
  119.  47.            {".msg",    "application/vnd.ms-outlook"},     
  120.  48.            {".ogg",    "audio/ogg"},     
  121.  49.            {".pdf",    "application/pdf"},     
  122.  50.            {".png",    "image/png"},     
  123.  51.            {".pps",    "application/vnd.ms-powerpoint"},     
  124.  52.            {".ppt",    "application/vnd.ms-powerpoint"},     
  125.  53.            {".pptx",   "application/vnd.openxmlformats-officedocument.presentationml.presentation"},     
  126.  54.            {".prop",   "text/plain"},     
  127.  55.            {".rc""text/plain"},     
  128.  56.            {".rmvb",   "audio/x-pn-realaudio"},     
  129.  57.            {".rtf",    "application/rtf"},     
  130.  58.            {".sh""text/plain"},     
  131.  59.            {".tar",    "application/x-tar"},        
  132.  60.            {".tgz",    "application/x-compressed"},      
  133.  61.            {".txt",    "text/plain"},     
  134.  62.            {".wav",    "audio/x-wav"},     
  135.  63.            {".wma",    "audio/x-ms-wma"},     
  136.  64.            {".wmv",    "audio/x-ms-wmv"},     
  137.  65.            {".wps",    "application/vnd.ms-works"},     
  138.  66.            {".xml",    "text/plain"},     
  139.  67.            {".z",  "application/x-compress"},     
  140.  68.            {".zip",    "application/x-zip-compressed"},     
  141.  69.            {"",        "*/*"}       
  142.  70.        };     
  143.  71.    
  144.  72.    
  145.  73.}    

猜你喜欢

转载自sunzeping.iteye.com/blog/1883695
今日推荐