现在的位置: 首页 > 综合 > 正文

调用系统的方法,打开文件的源代码

2018年06月09日 ⁄ 综合 ⁄ 共 4609字 ⁄ 字号 评论关闭
根据文件的类型,调用系统内对应的activity打开相应的文件
private void openFile(File file)
{
    Intent intent = new Intent();
    
    //设置这个intent的activity的运行跳转方式:将Activity放到一个新的Task中
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
     //设置intent Action 属性
     //ACTION_VIEW:用于显示用户的数据。比较通用,会根据用户的数据类型打开相应的Activity
     intent.setAction(android.content.Intent.ACTION_VIEW);
    
    //设置当前文件的Uri和文件类型,以方便条用系统内的相应activity
    intent.setDataAndType(Uri.fromFile(file), getMIMEType(file));
    startActivity(intent);
 }

//获取文件类型
private String getMIMEType(File file)
{
    	String type = "";
    	String fileName = file.getName();
    	//取出文件后缀名并转成小写
    	String  fileEnds = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length()).toLowerCase();
    	
    	//音频文件
    	if(fileEnds.equals("m4a")||fileEnds.equals("mp3")||fileEnds.equals("mid")||fileEnds.equals("xmf")||fileEnds.equals("ogg")||fileEnds.equals("wav"))
    	{
    		type = "audio/*";// 系统将列出所有可能打开音频文件的程序选择器
    	}
    	else if(fileEnds.equals("3gp")||fileEnds.equals("mp4"))
    	{//视频文件,此处不全,可以根据下面的媒体类型表自行扩充
    		type = "video/*";// 系统将列出所有可能打开视频文件的程序选择器
    	}
    	else if(fileEnds.equals("jpg")||fileEnds.equals("gif")||fileEnds.equals("png")||fileEnds.equals("jpeg")||fileEnds.equals("bmp"))
    	{//图像文件
    		type = "image/*";// 系统将列出所有可能打开图片文件的程序选择器
    	}
    	else
    	{
    		type = "*/*"; // 系统将列出所有可能打开该文件的程序选择器
    	}
    	return type;
    }

 

MIME类型:Multipurpose Internet Mail Extensions:互联网媒体类型

内容类型(Content-Type),这个头部领域用于指定消息的类型。一般以下面的形式出现。

Content-Type: [type]/[subtype]; parameter

type有下面的形式。

  • Text:用于标准化地表示的文本信息,文本消息可以是多种字符集和或者多种格式的;
  • Multipart:用于连接消息体的多个部分构成一个消息,这些部分可以是不同类型的数据;
  • Application:用于传输应用程序数据或者二进制数据;
  • Message:用于包装一个E-mail消息;
  • Image:用于传输静态图片数据;
  • Audio:用于传输音频或者音声数据;
  • Video:用于传输动态影像数据,可以是与音频编辑在一起的视频数据格式。

subtype用于指定type的详细形式。content-type/subtype配对的集合和与此相关的参数,将随着时间而增长。

 

private final String[][] MIME_MapTable={ 
            //{后缀名,MIME类型} 
            {".3gp",    "video/3gpp"}, 
            {".apk",    "application/vnd.android.package-archive"}, 
            {".asf",    "video/x-ms-asf"}, 
            {".avi",    "video/x-msvideo"}, 
            {".bin",    "application/octet-stream"}, 
            {".bmp",    "image/bmp"}, 
            {".c",  "text/plain"}, 
            {".class",  "application/octet-stream"}, 
            {".conf",   "text/plain"}, 
            {".cpp",    "text/plain"}, 
            {".doc",    "application/msword"}, 
            {".docx",   "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}, 
            {".xls",    "application/vnd.ms-excel"},  
            {".xlsx",   "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}, 
            {".exe",    "application/octet-stream"}, 
            {".gif",    "image/gif"}, 
            {".gtar",   "application/x-gtar"}, 
            {".gz", "application/x-gzip"}, 
            {".h",  "text/plain"}, 
            {".htm",    "text/html"}, 
            {".html",   "text/html"}, 
            {".jar",    "application/java-archive"}, 
            {".java",   "text/plain"}, 
            {".jpeg",   "image/jpeg"}, 
            {".jpg",    "image/jpeg"}, 
            {".js", "application/x-javascript"}, 
            {".log",    "text/plain"}, 
            {".m3u",    "audio/x-mpegurl"}, 
            {".m4a",    "audio/mp4a-latm"}, 
            {".m4b",    "audio/mp4a-latm"}, 
            {".m4p",    "audio/mp4a-latm"}, 
            {".m4u",    "video/vnd.mpegurl"}, 
            {".m4v",    "video/x-m4v"},  
            {".mov",    "video/quicktime"}, 
            {".mp2",    "audio/x-mpeg"}, 
            {".mp3",    "audio/x-mpeg"}, 
            {".mp4",    "video/mp4"}, 
            {".mpc",    "application/vnd.mpohun.certificate"},        
            {".mpe",    "video/mpeg"},   
            {".mpeg",   "video/mpeg"},   
            {".mpg",    "video/mpeg"},   
            {".mpg4",   "video/mp4"},    
            {".mpga",   "audio/mpeg"}, 
            {".msg",    "application/vnd.ms-outlook"}, 
            {".ogg",    "audio/ogg"}, 
            {".pdf",    "application/pdf"}, 
            {".png",    "image/png"}, 
            {".pps",    "application/vnd.ms-powerpoint"}, 
            {".ppt",    "application/vnd.ms-powerpoint"}, 
            {".pptx",   "application/vnd.openxmlformats-officedocument.presentationml.presentation"}, 
            {".prop",   "text/plain"}, 
            {".rc", "text/plain"}, 
            {".rmvb",   "audio/x-pn-realaudio"}, 
            {".rtf",    "application/rtf"}, 
            {".sh", "text/plain"}, 
            {".tar",    "application/x-tar"},    
            {".tgz",    "application/x-compressed"},  
            {".txt",    "text/plain"}, 
            {".wav",    "audio/x-wav"}, 
            {".wma",    "audio/x-ms-wma"}, 
            {".wmv",    "audio/x-ms-wmv"}, 
            {".wps",    "application/vnd.ms-works"}, 
            {".xml",    "text/plain"}, 
            {".z",  "application/x-compress"}, 
            {".zip",    "application/x-zip-compressed"}, 
            {"",        "*/*"}   
        }; 
 

抱歉!评论已关闭.