zoukankan      html  css  js  c++  java
  • Android中调用系统所装的软件打开文件(转)

     

    Android中调用系统所装的软件打开文件(转)

    在应用中如何调用系统所装的软件打开一个文件,这是我们经常碰到的问题,下面是我所用到的一种方法,和大家一起分享一下!

    这个是打开文件的一个方法:

    Java代码  
    1. /** 
    2.  * 打开文件 
    3.  * @param file 
    4.  */  
    5. private void openFile(File file){  
    6.       
    7.     Intent intent = new Intent();  
    8.     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
    9.     //设置intent的Action属性  
    10.     intent.setAction(Intent.ACTION_VIEW);  
    11.     //获取文件file的MIME类型  
    12.     String type = getMIMEType(file);  
    13.     //设置intent的data和Type属性。  
    14.     intent.setDataAndType(/*uri*/Uri.fromFile(file), type);  
    15.     //跳转  
    16.     startActivity(intent);    
    17.       
    18. }  
    19.   
    20. /** 
    21.  * 根据文件后缀名获得对应的MIME类型。 
    22.  * @param file 
    23.  */  
    24. private String getMIMEType(File file) {  
    25.       
    26.     String type="*/*";  
    27.     String fName = file.getName();  
    28.     //获取后缀名前的分隔符"."在fName中的位置。  
    29.     int dotIndex = fName.lastIndexOf(".");  
    30.     if(dotIndex < 0){  
    31.         return type;  
    32.     }  
    33.     /* 获取文件的后缀名 */  
    34.     String end=fName.substring(dotIndex,fName.length()).toLowerCase();  
    35.     if(end=="")return type;  
    36.     //在MIME和文件类型的匹配表中找到对应的MIME类型。  
    37.     for(int i=0;i<MIME_MapTable.length;i++){ //MIME_MapTable??在这里你一定有疑问,这个MIME_MapTable是什么?  
    38.         if(end.equals(MIME_MapTable[i][0]))  
    39.             type = MIME_MapTable[i][1];  
    40.     }         
    41.     return type;  
    42. }  
     

     MIME_MapTable是所有文件的后缀名所对应的MIME类型的一个String数组:

    Java代码  
    1. private final String[][] MIME_MapTable={  
    2.             //{后缀名, MIME类型}  
    3.             {".3gp",    "video/3gpp"},  
    4.             {".apk",    "application/vnd.android.package-archive"},  
    5.             {".asf",    "video/x-ms-asf"},  
    6.             {".avi",    "video/x-msvideo"},  
    7.             {".bin",    "application/octet-stream"},  
    8.             {".bmp",    "image/bmp"},  
    9.             {".c",  "text/plain"},  
    10.             {".class",  "application/octet-stream"},  
    11.             {".conf",   "text/plain"},  
    12.             {".cpp",    "text/plain"},  
    13.             {".doc",    "application/msword"},  
    14.             {".docx",   "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},  
    15.             {".xls",    "application/vnd.ms-excel"},   
    16.             {".xlsx",   "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},  
    17.             {".exe",    "application/octet-stream"},  
    18.             {".gif",    "image/gif"},  
    19.             {".gtar",   "application/x-gtar"},  
    20.             {".gz", "application/x-gzip"},  
    21.             {".h",  "text/plain"},  
    22.             {".htm",    "text/html"},  
    23.             {".html",   "text/html"},  
    24.             {".jar",    "application/java-archive"},  
    25.             {".java",   "text/plain"},  
    26.             {".jpeg",   "image/jpeg"},  
    27.             {".jpg",    "image/jpeg"},  
    28.             {".js", "application/x-javascript"},  
    29.             {".log",    "text/plain"},  
    30.             {".m3u",    "audio/x-mpegurl"},  
    31.             {".m4a",    "audio/mp4a-latm"},  
    32.             {".m4b",    "audio/mp4a-latm"},  
    33.             {".m4p",    "audio/mp4a-latm"},  
    34.             {".m4u",    "video/vnd.mpegurl"},  
    35.             {".m4v",    "video/x-m4v"},   
    36.             {".mov",    "video/quicktime"},  
    37.             {".mp2",    "audio/x-mpeg"},  
    38.             {".mp3",    "audio/x-mpeg"},  
    39.             {".mp4",    "video/mp4"},  
    40.             {".mpc",    "application/vnd.mpohun.certificate"},        
    41.             {".mpe",    "video/mpeg"},    
    42.             {".mpeg",   "video/mpeg"},    
    43.             {".mpg",    "video/mpeg"},    
    44.             {".mpg4",   "video/mp4"},     
    45.             {".mpga",   "audio/mpeg"},  
    46.             {".msg",    "application/vnd.ms-outlook"},  
    47.             {".ogg",    "audio/ogg"},  
    48.             {".pdf",    "application/pdf"},  
    49.             {".png",    "image/png"},  
    50.             {".pps",    "application/vnd.ms-powerpoint"},  
    51.             {".ppt",    "application/vnd.ms-powerpoint"},  
    52.             {".pptx",   "application/vnd.openxmlformats-officedocument.presentationml.presentation"},  
    53.             {".prop",   "text/plain"},  
    54.             {".rc", "text/plain"},  
    55.             {".rmvb",   "audio/x-pn-realaudio"},  
    56.             {".rtf",    "application/rtf"},  
    57.             {".sh", "text/plain"},  
    58.             {".tar",    "application/x-tar"},     
    59.             {".tgz",    "application/x-compressed"},   
    60.             {".txt",    "text/plain"},  
    61.             {".wav",    "audio/x-wav"},  
    62.             {".wma",    "audio/x-ms-wma"},  
    63.             {".wmv",    "audio/x-ms-wmv"},  
    64.             {".wps",    "application/vnd.ms-works"},  
    65.             {".xml",    "text/plain"},  
    66.             {".z",  "application/x-compress"},  
    67.             {".zip",    "application/x-zip-compressed"},  
    68.             {"",        "*/*"}    
    69.         };  

     这个MIME类型可能不够完整,你有要补充的吗?

    原文:http://tonysun3544.iteye.com/blog/1265884

  • 相关阅读:
    IT面试技巧(2)
    mySQL学习入门教程——4.内置函数
    weight decay (权值衰减)
    c++读取文件目录
    caffe 卷积层的运算
    一个物体多个标签的问题
    python caffe 在师兄的代码上修改成自己风格的代码
    caffe 细节
    vim让一些不可见的字符显示出来吧
    python 读写文件
  • 原文地址:https://www.cnblogs.com/geniusxjq/p/4079370.html
Copyright © 2011-2022 走看看