zoukankan      html  css  js  c++  java
  • android获取文件getMimeType的两种方法

    方法1:

    import java.util.Locale;
    
    private static String getSuffix(File file) {
                if (file == null || !file.exists() || file.isDirectory()) {
                    return null;
                }
                String fileName = file.getName();
                if (fileName.equals("") || fileName.endsWith(".")) {
                    return null;
                }
                int index = fileName.lastIndexOf(".");
                if (index != -1) {
                    return fileName.substring(index + 1).toLowerCase(Locale.US);
                } else {
                    return null;
                }
        }
    
        public static String getMimeType(File file){
              String suffix = getSuffix(file);
                if (suffix == null) {
                    return "file/*";
                }
                String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(suffix);
                if (type != null || !type.isEmpty()) {
                    return type;
                }
                return "file/*";
    }
    

    方法2:

    public static String getMimeType(String filePath) {
        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        String mime = "text/plain";
        if (filePath != null) {
             try {
                 mmr.setDataSource(filePath);
                 mime = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE);
             } catch (IllegalStateException e) {
    	    return mime;
             } catch (IllegalArgumentException e) {
    	    return mime;
    	} catch (RuntimeException e) {
    	    return mime;
    	}
        }
        return mime;
    }




  • 相关阅读:
    百奥谷
    3月13日火箭VS老鹰
    百度 hi 下载地址(内测版,正式版)
    中兴u980
    2008年清明节放假通知
    cyp740703 一个女人的自白
    黄唇鱼
    3月9日火箭vs黄蜂
    3月3日火箭vs掘金
    百度hi邀请码
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5089525.html
Copyright © 2011-2022 走看看