Android to determine the type of file

Source: Internet
Author: User

Import Java.util.hashmap;import java.util.iterator;/** * Determine the type of file */public class Mediafileutil {public static String SF    Ileextensions;    Audio 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;    private static final int first_audio_file_type = File_type_mp3;    private static final int last_audio_file_type = File_type_ogg;    MIDI 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 public static final int file_type_mp4 = 21; Publicstatic 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;    private static final int first_video_file_type = File_type_mp4;    private static final int last_video_file_type = file_type_wmv;    Image 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;    private static final int first_image_file_type = File_type_jpeg;    private static final int last_image_file_type = file_type_wbmp;    Playlist public static final int file_type_m3u = 41;    public static final int file_type_pls = 42;    public static final int file_type_wpl = 43;    private static final int first_playlist_file_type = file_type_m3u; private static final int last_playlist_file_type = FILE_TYPE_WPL;        Static inner class static classes Mediafiletype {int fileType;        String MimeType;            Mediafiletype (int fileType, String mimeType) {this.filetype = FileType;        This.mimetype = MimeType; }} private static hashmap<string, mediafiletype> Sfiletypemap = new hashmap<string, mediafilety    Pe> ();    private static hashmap<string, integer> Smimetypemap = new hashmap<string, integer> (); static void Addfiletype (string extension, int fileType, string mimeType) {sfiletypemap.put (extension, new Mediafil        EType (FileType, MimeType));    Smimetypemap.put (MimeType, New Integer (FileType));        } static {Addfiletype ("MP3", File_type_mp3, "audio/mpeg");        Addfiletype ("M4A", file_type_m4a, "Audio/mp4");        Addfiletype ("WAV", File_type_wav, "audio/x-wav");        Addfiletype ("AMR", File_type_amr, "audio/amr"); Addfiletype ("AWB", FILE_TYPE_AWB, "auDIO/AMR-WB ");        Addfiletype ("WMA", File_type_wma, "audio/x-ms-wma");        Addfiletype ("OGG", File_type_ogg, "Application/ogg");        Addfiletype ("MID", 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 ("MP4", File_type_mp4, "Video/mp4");        Addfiletype ("M4V", file_type_m4v, "Video/mp4");        Addfiletype ("3GP", FILE_TYPE_3GPP, "VIDEO/3GPP");        Addfiletype ("3GPP", FILE_TYPE_3GPP, "VIDEO/3GPP");        Addfiletype ("3g2", File_type_3gpp2, "video/3gpp2");        Addfiletype ("3GPP2", File_type_3gpp2, "video/3gpp2");        Addfiletype ("WMV", file_type_wmv, "video/x-ms-wmv");        Addfiletype ("JPG", File_type_jpeg, "image/jpeg");        Addfiletype ("JPEG", File_type_jpeg, "image/jpeg");   Addfiletype ("GIF", File_type_gif, "image/gif");     Addfiletype ("PNG", File_type_png, "image/png");        Addfiletype ("BMP", File_type_bmp, "image/x-ms-bmp");        Addfiletype ("WBMP", File_type_wbmp, "image/vnd.wap.wbmp");        Addfiletype ("m3u", file_type_m3u, "Audio/x-mpegurl");        Addfiletype ("PLS", File_type_pls, "Audio/x-scpls");        Addfiletype ("WPL", FILE_TYPE_WPL, "APPLICATION/VND.MS-WPL");        Compute file extensions list for native Media Scanner StringBuilder builder = new StringBuilder ();        iterator<string> Iterator = Sfiletypemap.keyset (). Iterator ();            while (Iterator.hasnext ()) {if (builder.length () > 0) {builder.append (', ');        } builder.append (Iterator.next ());    } sfileextensions = Builder.tostring ();    public static final String unknown_string = "<unknown>";                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);                } 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 Mediafiletype Getfiletype (String path) {int lastdot = Path.lastindexof (".");        if (Lastdot < 0) return null;    Return Sfiletypemap.get (path.substring (Lastdot + 1). toUpperCase ()); }//According to the video file path determine the file type public static Boolean Isvideofiletype (String path) {Mediafiletype type = getfiletype (path);        if (null! = Type) {return isvideofiletype (Type.filetype);    } return false; }//According to the audio file path determine the file type public static Boolean Isaudiofiletype (String path) {Mediafiletype type = Getfiletype (PA        TH);        if (null! = Type) {return isaudiofiletype (Type.filetype);    } return false; }//view file type according to MIME type public static int getfiletypeformimetype (String mimeType) {Integer value = SMIMETYPEMAP.G        ET (MimeType);    return (value = = null? 0:value.intvalue ()); }//According to the picture file path determine the file type public static Boolean Isimagefiletype (String path) {Mediafiletype type = Getfiletype (PA        TH);        if (null! = Type) {return isimagefiletype (Type.filetype);    } return false; }}

  

Android to determine the type of file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.