获取文件类型的mimeType——MediaFile类分析

public class MediaFile {

            //给每一个文件类型设置一个int型标志
             // Audio file types 声音文件类型
             public static final int FILE_TYPE_MP3     = 1;
             public static final int FILE_TYPE_M4A     = 2;
             public static final int FILE_TYPE_WAV     = 3;
             public static final int FILE_TYPE_AMR     = 4;
             public static final int FILE_TYPE_AWB     = 5;
             public static final int FILE_TYPE_WMA     = 6;
             public static final int FILE_TYPE_OGG     = 7;
             public static final int FILE_TYPE_AAC     = 8;
             public static final int FILE_TYPE_MKA     = 9;
             public static final int FILE_TYPE_FLAC    = 10;
             private static final int FIRST_AUDIO_FILE_TYPE = FILE_TYPE_MP3;
             private static final int LAST_AUDIO_FILE_TYPE = FILE_TYPE_FLAC;

             // MIDI file types 乐器数字接口格式文件类型
             public static final int FILE_TYPE_MID     = 11;
             public static final int FILE_TYPE_SMF     = 12;
             public static final int FILE_TYPE_IMY     = 13;
             private static final int FIRST_MIDI_FILE_TYPE = FILE_TYPE_MID;
             private static final int LAST_MIDI_FILE_TYPE = FILE_TYPE_IMY;

             // Video file types 视频文件类型
             public static final int FILE_TYPE_MP4     = 21;
             public static final int FILE_TYPE_M4V     = 22;
             public static final int FILE_TYPE_3GPP    = 23;
             public static final int FILE_TYPE_3GPP2   = 24;
             public static final int FILE_TYPE_WMV     = 25;
             public static final int FILE_TYPE_ASF     = 26;
             public static final int FILE_TYPE_MKV     = 27;
             public static final int FILE_TYPE_MP2TS   = 28;
             public static final int FILE_TYPE_AVI     = 29;
             public static final int FILE_TYPE_WEBM    = 30;
             private static final int FIRST_VIDEO_FILE_TYPE = FILE_TYPE_MP4;
             private static final int LAST_VIDEO_FILE_TYPE = FILE_TYPE_WEBM;

             // More video file types 更多视频文件类型
             public static final int FILE_TYPE_MP2PS   = 200;
             private static final int FIRST_VIDEO_FILE_TYPE2 = FILE_TYPE_MP2PS;
             private static final int LAST_VIDEO_FILE_TYPE2 = FILE_TYPE_MP2PS;

             // Image file types 图片文件类型
             public static final int FILE_TYPE_JPEG    = 31;
             public static final int FILE_TYPE_GIF     = 32;
             public static final int FILE_TYPE_PNG     = 33;
             public static final int FILE_TYPE_BMP     = 34;
             public static final int FILE_TYPE_WBMP    = 35;
             public static final int FILE_TYPE_WEBP    = 36;
             private static final int FIRST_IMAGE_FILE_TYPE = FILE_TYPE_JPEG;
             private static final int LAST_IMAGE_FILE_TYPE = FILE_TYPE_WEBP;

             // Playlist file types 播放列表文件类型
             public static final int FILE_TYPE_M3U      = 41;
             public static final int FILE_TYPE_PLS      = 42;
             public static final int FILE_TYPE_WPL      = 43;
             public static final int FILE_TYPE_HTTPLIVE = 44;

             private static final int FIRST_PLAYLIST_FILE_TYPE = FILE_TYPE_M3U;
             private static final int LAST_PLAYLIST_FILE_TYPE = FILE_TYPE_HTTPLIVE;

             // Drm file types 数字权限管理文件类型
             public static final int FILE_TYPE_FL      = 51;
             private static final int FIRST_DRM_FILE_TYPE = FILE_TYPE_FL;
             private static final int LAST_DRM_FILE_TYPE = FILE_TYPE_FL;

             // Other popular file types 其他流行的文件类型
            public static final int FILE_TYPE_TEXT          = 100;
            public static final int FILE_TYPE_HTML          = 101;
            public static final int FILE_TYPE_PDF           = 102;
            public static final int FILE_TYPE_XML           = 103;
            public static final int FILE_TYPE_MS_WORD       = 104;
            public static final int FILE_TYPE_MS_EXCEL      = 105;
            public static final int FILE_TYPE_MS_POWERPOINT = 106;
            public static final int FILE_TYPE_ZIP           = 107;

            /*
            定义一个静态内部类MediaFileType,
            含有两个成员变量,分别是文件类型和mime类型
             */
            public static class MediaFileType {
                public final int fileType;
                public final String mimeType;

                //内部类的构造方法
                MediaFileType(int fileType, String Type) {
                    this.fileType = fileType;
                    this.mimeType = mimeType;
                }
            }
            //HashMap<文件后缀,MediaFileType对象>
            private static final HashMap<String, MediaFileType> sFileTypeMap
                    = new HashMap<String, MediaFileType>();
            //HashMap<MimeType,Interger.valueOf(fileType)>
            private static final HashMap<String, Integer> sMimeTypeMap
                    = new HashMap<String, Integer>();
            // maps file extension to MTP format code 文件后缀到媒体传输协议格式编码
            private static final HashMap<String, Integer> sFileTypeToFormatMap
                    = new HashMap<String, Integer>();
            // maps mime type to MTP format code mime类型到媒体传输协议格式编码
            private static final HashMap<String, Integer> sMimeTypeToFormatMap
                    = new HashMap<String, Integer>();
            // maps MTP format code to mime type 媒体传输协议格式编码到mime类型
            private static final HashMap<Integer, String> sFormatToMimeTypeMap
                    = new HashMap<Integer, String>();

            //添加文件类型
            static void addFileType(String extension, int fileType, String mimeType) {
                sFileTypeMap.put(extension, new MediaFileType(fileType, mimeType));
                sMimeTypeMap.put(mimeType, Integer.valueOf(fileType));//把int型转换成Integer
            }

            static void addFileType(String extension, int fileType, String mimeType, int mtpFormatCode) {
                addFileType(extension, fileType, mimeType);
                sFileTypeToFormatMap.put(extension, Integer.valueOf(mtpFormatCode));
                sMimeTypeToFormatMap.put(mimeType, Integer.valueOf(mtpFormatCode));
                sFormatToMimeTypeMap.put(mtpFormatCode, mimeType);
            }

            //WMA:Windows Media Audio
            private static boolean isWMAEnabled() {
                List<AudioDecoder> decoders = DecoderCapabilities.getAudioDecoders();
                int count = decoders.size();
                for (int i = 0; i < count; i++) {
                    AudioDecoder decoder = decoders.get(i);
                    if (decoder == AudioDecoder.AUDIO_DECODER_WMA) {
                        return true;
                    }
                }
                return false;
            }
                //Windows Media Video
            private static boolean isWMVEnabled() {
                List<VideoDecoder> decoders = DecoderCapabilities.getVideoDecoders();
                int count = decoders.size();
                for (int i = 0; i < count; i++) {
                    VideoDecoder decoder = decoders.get(i);
                    if (decoder == VideoDecoder.VIDEO_DECODER_WMV) {
                        return true;
                    }
                }
                return false;
            }

            //静态代码块,将文件后缀,文件类型标志,文件mimeType,[MTP常量]加入到HashMap中
            static {
                addFileType("MP3", FILE_TYPE_MP3, "audio/mpeg", MtpConstants.FORMAT_MP3);
                addFileType("MPGA", FILE_TYPE_MP3, "audio/mpeg", MtpConstants.FORMAT_MP3);
                addFileType("M4A", FILE_TYPE_M4A, "audio/mp4", MtpConstants.FORMAT_MPEG);
                addFileType("WAV", FILE_TYPE_WAV, "audio/x-wav", MtpConstants.FORMAT_WAV);
                addFileType("AMR", FILE_TYPE_AMR, "audio/amr");
                addFileType("AWB", FILE_TYPE_AWB, "audio/amr-wb");
                if (isWMAEnabled()) {
                    addFileType("WMA", FILE_TYPE_WMA, "audio/x-ms-wma", MtpConstants.FORMAT_WMA);
                }
                addFileType("OGG", FILE_TYPE_OGG, "audio/ogg", MtpConstants.FORMAT_OGG);
                addFileType("OGG", FILE_TYPE_OGG, "application/ogg", MtpConstants.FORMAT_OGG);
                addFileType("OGA", FILE_TYPE_OGG, "application/ogg", MtpConstants.FORMAT_OGG);
                addFileType("AAC", FILE_TYPE_AAC, "audio/aac", MtpConstants.FORMAT_AAC);
                addFileType("AAC", FILE_TYPE_AAC, "audio/aac-adts", MtpConstants.FORMAT_AAC);
                addFileType("MKA", FILE_TYPE_MKA, "audio/x-matroska");

                addFileType("MID", FILE_TYPE_MID, "audio/midi");
                addFileType("MIDI", FILE_TYPE_MID, "audio/midi");
                addFileType("XMF", FILE_TYPE_MID, "audio/midi");
                addFileType("RTTTL", FILE_TYPE_MID, "audio/midi");
                addFileType("SMF", FILE_TYPE_SMF, "audio/sp-midi");
                addFileType("IMY", FILE_TYPE_IMY, "audio/imelody");
                addFileType("RTX", FILE_TYPE_MID, "audio/midi");
                addFileType("OTA", FILE_TYPE_MID, "audio/midi");
                addFileType("MXMF", FILE_TYPE_MID, "audio/midi");

                addFileType("MPEG", FILE_TYPE_MP4, "video/mpeg", MtpConstants.FORMAT_MPEG);
                addFileType("MPG", FILE_TYPE_MP4, "video/mpeg", MtpConstants.FORMAT_MPEG);
                addFileType("MP4", FILE_TYPE_MP4, "video/mp4", MtpConstants.FORMAT_MPEG);
                addFileType("M4V", FILE_TYPE_M4V, "video/mp4", MtpConstants.FORMAT_MPEG);
                addFileType("3GP", FILE_TYPE_3GPP, "video/3gpp",  MtpConstants.FORMAT_3GP_CONTAINER);
                addFileType("3GPP", FILE_TYPE_3GPP, "video/3gpp", MtpConstants.FORMAT_3GP_CONTAINER);
                addFileType("3G2", FILE_TYPE_3GPP2, "video/3gpp2", MtpConstants.FORMAT_3GP_CONTAINER);
                addFileType("3GPP2", FILE_TYPE_3GPP2, "video/3gpp2", MtpConstants.FORMAT_3GP_CONTAINER);
                addFileType("MKV", FILE_TYPE_MKV, "video/x-matroska");
                addFileType("WEBM", FILE_TYPE_WEBM, "video/webm");
                addFileType("TS", FILE_TYPE_MP2TS, "video/mp2ts");
                addFileType("AVI", FILE_TYPE_AVI, "video/avi");

                if (isWMVEnabled()) {
                    addFileType("WMV", FILE_TYPE_WMV, "video/x-ms-wmv", MtpConstants.FORMAT_WMV);
                    addFileType("ASF", FILE_TYPE_ASF, "video/x-ms-asf");
                }

                addFileType("JPG", FILE_TYPE_JPEG, "image/jpeg", MtpConstants.FORMAT_EXIF_JPEG);
                addFileType("JPEG", FILE_TYPE_JPEG, "image/jpeg", MtpConstants.FORMAT_EXIF_JPEG);
                addFileType("GIF", FILE_TYPE_GIF, "image/gif", MtpConstants.FORMAT_GIF);
                addFileType("PNG", FILE_TYPE_PNG, "image/png", MtpConstants.FORMAT_PNG);
                addFileType("BMP", FILE_TYPE_BMP, "image/x-ms-bmp", MtpConstants.FORMAT_BMP);
                addFileType("WBMP", FILE_TYPE_WBMP, "image/vnd.wap.wbmp");
                addFileType("WEBP", FILE_TYPE_WEBP, "image/webp");

                addFileType("M3U", FILE_TYPE_M3U, "audio/x-mpegurl", MtpConstants.FORMAT_M3U_PLAYLIST);
                addFileType("M3U", FILE_TYPE_M3U, "application/x-mpegurl", MtpConstants.FORMAT_M3U_PLAYLIST);
                addFileType("PLS", FILE_TYPE_PLS, "audio/x-scpls", MtpConstants.FORMAT_PLS_PLAYLIST);
                addFileType("WPL", FILE_TYPE_WPL, "application/vnd.ms-wpl", MtpConstants.FORMAT_WPL_PLAYLIST);
                addFileType("M3U8", FILE_TYPE_HTTPLIVE, "application/vnd.apple.mpegurl");
                addFileType("M3U8", FILE_TYPE_HTTPLIVE, "audio/mpegurl");
                addFileType("M3U8", FILE_TYPE_HTTPLIVE, "audio/x-mpegurl");

                addFileType("FL", FILE_TYPE_FL, "application/x-android-drm-fl");

                addFileType("TXT", FILE_TYPE_TEXT, "text/plain", MtpConstants.FORMAT_TEXT);
                addFileType("HTM", FILE_TYPE_HTML, "text/html", MtpConstants.FORMAT_HTML);
                addFileType("HTML", FILE_TYPE_HTML, "text/html", MtpConstants.FORMAT_HTML);
                addFileType("PDF", FILE_TYPE_PDF, "application/pdf");
                addFileType("DOC", FILE_TYPE_MS_WORD, "application/msword", MtpConstants.FORMAT_MS_WORD_DOCUMENT);
                addFileType("XLS", FILE_TYPE_MS_EXCEL, "application/vnd.ms-excel", MtpConstants.FORMAT_MS_EXCEL_SPREADSHEET);
                addFileType("PPT", FILE_TYPE_MS_POWERPOINT, "application/mspowerpoint", MtpConstants.FORMAT_MS_POWERPOINT_PRESENTATION);
                addFileType("FLAC", FILE_TYPE_FLAC, "audio/flac", MtpConstants.FORMAT_FLAC);
                addFileType("ZIP", FILE_TYPE_ZIP, "application/zip");
                addFileType("MPG", FILE_TYPE_MP2PS, "video/mp2p");
                addFileType("MPEG", FILE_TYPE_MP2PS, "video/mp2p");
            }

            //判断文件类型是不是音频文件类型
            public static boolean isAudioFileType(int fileType) {
                return ((fileType >= FIRST_AUDIO_FILE_TYPE &&
                        fileType <= LAST_AUDIO_FILE_TYPE) ||
                        (fileType >= FIRST_MIDI_FILE_TYPE &&
                        fileType <= LAST_MIDI_FILE_TYPE));
            }

            //判断文件类型是不是视频文件类型
            public static boolean isVideoFileType(int fileType) {
                return (fileType >= FIRST_VIDEO_FILE_TYPE &&
                        fileType <= LAST_VIDEO_FILE_TYPE)
                    || (fileType >= FIRST_VIDEO_FILE_TYPE2 &&
                        fileType <= LAST_VIDEO_FILE_TYPE2);
            }

            //判断文件类型是不是图片文件类型
            public static boolean isImageFileType(int fileType) {
                return (fileType >= FIRST_IMAGE_FILE_TYPE &&
                        fileType <= LAST_IMAGE_FILE_TYPE);
            }

            //判断文件类型是不是播放列表类型
            public static boolean isPlayListFileType(int fileType) {
                return (fileType >= FIRST_PLAYLIST_FILE_TYPE &&
                        fileType <= LAST_PLAYLIST_FILE_TYPE);
            }

            //判断文件类型是不是数字权限管理文件类型
            public static boolean isDrmFileType(int fileType) {
                return (fileType >= FIRST_DRM_FILE_TYPE &&
                        fileType <= LAST_DRM_FILE_TYPE);
            }

            /*
            这个方法是核心,通过文件的路径得到文件的后缀
            然后根据后缀从HashMap中得到MediaFielType对象,该对象封装了
            FileType和mimeType
             */
            public static MediaFileType getFileType(String path) {
                int lastDot = path.lastIndexOf('.');
                if (lastDot < 0)
                    return null;
                return sFileTypeMap.get(path.substring(lastDot + 1).toUpperCase(Locale.ROOT));
            }

            //判断文件是不是Mime类型媒体格式
            public static boolean isMimeTypeMedia(String mimeType) {
                int fileType = getFileTypeForMimeType(mimeType);
                return isAudioFileType(fileType) || isVideoFileType(fileType)
                        || isImageFileType(fileType) || isPlayListFileType(fileType);
            }

            // generates a title based on file name 根据文件名字生成一个title
            public static String getFileTitle(String path) {
                // extract file name after last slash 提取最后一个分割符/后的内容
                int lastSlash = path.lastIndexOf('/');
                if (lastSlash >= 0) {
                    lastSlash++;
                    if (lastSlash < path.length()) {
                        path = path.substring(lastSlash);
                    }
                }
                // truncate the file extension (if any) 去除文件的后缀(如果有)
                int lastDot = path.lastIndexOf('.');
                if (lastDot > 0) {
                    path = path.substring(0, lastDot);
                }
                return path;
            }

            //根据mimeType得到文件类型(FileType)
            public static int getFileTypeForMimeType(String mimeType) {
                Integer value = sMimeTypeMap.get(mimeType);
                return (value == null ? 0 : value.intValue());
            }

            //根据文件路径得到文件的mime类型
            public static String getMimeTypeForFile(String path) {
                MediaFileType mediaFileType = getFileType(path);
                return (mediaFileType == null ? null : mediaFileType.mimeType);
            }

            public static int getFormatCode(String fileName, String mimeType) {
                if (mimeType != null) {
                    Integer value = sMimeTypeToFormatMap.get(mimeType);
                    if (value != null) {
                        return value.intValue();
                    }
                }
                int lastDot = fileName.lastIndexOf('.');
                if (lastDot > 0) {
                    String extension = fileName.substring(lastDot + 1).toUpperCase(Locale.ROOT);
                    Integer value = sFileTypeToFormatMap.get(extension);
                    if (value != null) {
                        return value.intValue();
                    }
                }
                return MtpConstants.FORMAT_UNDEFINED;
            }

            public static String getMimeTypeForFormatCode(int formatCode) {
                return sFormatToMimeTypeMap.get(formatCode);
            }
        }

发布了88 篇原创文章 · 获赞 7 · 访问量 6万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览