smilevchy
5/23/2015 - 1:43 PM

FileUtil.java

public class FileUtil {
	private static FileUtil instance = null;
	
	private static List<ExtIcon> extIconList = null;
	
	private int[] icons = { R.drawable.ext_xls, R.drawable.cd_big_img_1, 
							R.drawable.ext_audio, R.drawable.ext_video, 
							R.drawable.ext_ppt, R.drawable.ext_rar, 
							R.drawable.ext_default, R.drawable.ext_doc,
							R.drawable.ext_pdf, R.drawable.ext_txt, 
							R.drawable.ext_grid, R.drawable.ext_csv, 
							R.drawable.ext_vcf
						  };
	
	private String[][] exts = { {".xls", ".xlsx"}, {".png", ".jpg", ".bmp", ".gif", ".jpeg", ".tnf", ".icon"}, 
								{".mp3"}, {".mpeg", ".mpg", ".gp3", ".swf", ".flv", ".rm", ".rmvb", ".wma", ".wmv", ".asf"},
								{".ppt", ".pptx"}, {".rar", ".zip", ".jar", ".war"},
								{".ini", ".html", ".xml", ".htm", ".js", ".properties", ".c", ".cpp", ".java", ".log"}, {".doc", ".doct", ".docx"},
								{".pdf"}, {".txt"}, 
								{".grid"}, {".csv"},
								{".vcf"}
							  };
	
	private String[] imgExts = {".png", ".jpg", ".bmp", ".gif", ".jpeg", ".tnf", ".icon"};
	
	private static 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"}, 
        {"",        "*/*"}   
    }; 
	
	
	private FileUtil() {
		
	}
	
	public static FileUtil getInstance() {
		if (null == instance) {
			instance = new FileUtil();
		}
		
		return instance;
	}
	
	public boolean isImage(String ext) {
		if(null == ext || "".equals(ext.trim())){
			return false;
		}
		
		ext = ext.trim().toLowerCase();
		for(String str : imgExts){
			if(ext.equals(str)){
				return true;
			}
		}
		
		return false;
	}
	
	/**
	 * 加载非图片类型文件的缩略图
	 * @param ext 文件扩展名
	 * @param imageView 图像对象
	 */
	public void loadThumbnailByExt(final String ext, final ImageView imageView) {
		if (null == imageView) {
			throw new RuntimeException("imageView is null !");
		}
		
		Resources res = imageView.getResources();
		
		if (null == ext || "".equals(ext.trim())){
			imageView.setImageDrawable(res.getDrawable(R.drawable.ext_default));
		}
		
		if (null == extIconList) {
			initExtIconList();
		}
		
		boolean find = false;
		
		for (ExtIcon e : extIconList) {
			String[] extArray = e.getExt();
			
			for(String s : extArray) {
				if (s.equals(ext)) {
					imageView.setImageDrawable(res.getDrawable(e.getIcon()));
					
					find = true;
					
					break;
				}
			}
			
			if (find) {
				break;
			}
		}
		
		if (!find) {
			imageView.setImageDrawable(res.getDrawable(R.drawable.ext_default));
		}
	}
	
	/**
	 * 打开本地文件
	 * @param file 本地文件对象
	 * @param context 上下文对象
	 */
	public void openLocalFile(File file, Context context) {
		Intent intent = new Intent(); 

		//设置intent的Action属性 
	    intent.setAction(Intent.ACTION_VIEW); 
	    
	    //获取文件file的MIME类型 
	    String type = getMIMEType(file.getName(), ""); 
	    
	    //设置intent的data和Type属性。 
	    intent.setDataAndType(Uri.fromFile(file), type); 
	    
	    //跳转 
	    context.startActivity(intent);
	}
	
	/**
	 * 获取某一文件类型对应的MIME类型
	 * @param fileName 文件名称
	 * @param defaultType 默认MIME类型
	 * @return 文件的MIME类型
	 */
	public String getMIMEType(String fileName, String defaultType) {
		String type = defaultType; 
	    
		//获取后缀名前的分隔符"."在fName中的位置。 
	    int dotIndex = fileName.lastIndexOf("."); 
	    
	    if (dotIndex < 0) { 
	        return type; 
	    } 
	    
	    /* 获取文件的后缀名*/ 
	    String end = fileName.substring(dotIndex,fileName.length()).toLowerCase(); 
	    
	    if (TextUtils.isEmpty(end)) {
	    	return type; 
	    }
	    
	    //在MIME和文件类型的匹配表中找到对应的MIME类型。 
	    for (int i = 0; i < MIME_MapTable.length; i++) { 
	        if (end.equals(MIME_MapTable[i][0])) {
	        	type = MIME_MapTable[i][1]; 
	        }
	    }
	    
	    return type; 
	}
	
	/**
	 * 获取文件后缀名
	 * @param path 文件路径名
	 * @return 文件后缀名
	 */
	public String getExt(String path) {
		if(null == path || "".equals(path.trim())){
			return null;
		}
		
		String ext = null;
		int index = path.lastIndexOf(".");
		if(0 < index){
			ext = path.substring(index);
		}
		
		return ext;
	}
	
	private void initExtIconList() {
		extIconList = new ArrayList<ExtIcon>();
		
		for (int i = 0, s = icons.length; i < s; i++) {
			ExtIcon extIcon = new ExtIcon();
			extIcon.setIcon(icons[i]);
			extIcon.setExt(exts[i]);
			
			extIconList.add(extIcon);
		}
	}
	
	private class ExtIcon {
		int icon ;
		String[] ext ;
		
		public int getIcon() {
			return icon;
		}
		
		public void setIcon(int icon) {
			this.icon = icon;
		}
		
		public String[] getExt() {
			return ext;
		}
		
		public void setExt(String[] ext) {
			this.ext = ext;
		}
	}
}