歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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

在做我們產品的時候,需要下載附件, 同時下載附件需要打開, 通過查閱一些資料發現,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.   
  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相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved