android 文件mimetype_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;
}
總結
以上是生活随笔為你收集整理的android 文件mimetype_android获取文件getMimeType的两种方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 抗日战争出了多少汉奸
- 下一篇: 女儿出嫁吃什么馅的水饺24岁包多少个好?