码迷,mamicode.com
首页 > 移动开发 > 详细

android获取文件getMimeType的两种方法

时间:2015-04-02 16:32:29      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

方法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;
}




android获取文件getMimeType的两种方法

标签:

原文地址:http://blog.csdn.net/chaihuasong/article/details/44832799

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!