Skip to content

Commit 65ac86b

Browse files
committed
使用mime来判断文件类型
1 parent 794319d commit 65ac86b

File tree

1 file changed

+19
-11
lines changed
  • richeditor/src/main/java/com/yuruiyin/richeditor/utils

1 file changed

+19
-11
lines changed

richeditor/src/main/java/com/yuruiyin/richeditor/utils/FileUtil.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import android.text.TextUtils;
1212
import android.util.Log;
1313

14+
import android.webkit.MimeTypeMap;
1415
import com.yuruiyin.richeditor.enumtype.FileTypeEnum;
1516

1617
import java.io.File;
@@ -31,19 +32,26 @@ public static String getFileType(String path) {
3132
return FileTypeEnum.STATIC_IMAGE;
3233
}
3334

34-
if (path.endsWith(".mp4") || path.endsWith(".avi")
35-
|| path.endsWith(".3gpp") || path.endsWith(".3gp") || path.startsWith(".mov")) {
36-
return FileTypeEnum.VIDEO;
37-
} else if (path.endsWith(".PNG") || path.endsWith(".png") || path.endsWith(".jpeg")
38-
|| path.endsWith(".jpg")
39-
|| path.endsWith(".webp") || path.endsWith(".WEBP") || path.endsWith(".JPEG")
40-
|| path.endsWith(".bmp")) {
35+
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(path);
36+
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
37+
38+
if (TextUtils.isEmpty(mimeType)) {
4139
return FileTypeEnum.STATIC_IMAGE;
42-
} else if (path.endsWith(".gif") || path.endsWith(".GIF")) {
40+
}
41+
42+
if (mimeType.contains("video")) {
43+
return FileTypeEnum.VIDEO;
44+
}
45+
46+
if (mimeType.contains("gif")) {
4347
return FileTypeEnum.GIF;
44-
} else if (path.endsWith(".mp3") || path.endsWith(".amr")
45-
|| path.endsWith(".aac") || path.endsWith(".war")
46-
|| path.endsWith(".flac") || path.endsWith(".lamr")) {
48+
}
49+
50+
if (mimeType.contains("image")) {
51+
return FileTypeEnum.STATIC_IMAGE;
52+
}
53+
54+
if (mimeType.contains("audio")) {
4755
return FileTypeEnum.AUDIO;
4856
}
4957

0 commit comments

Comments
 (0)