欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

android 文件mimetype_android获取文件getMimeType的两种方法

发布时间:2025/4/5 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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的两种方法的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。