Android determines the file type (video, audio, image, etc)

Source: Internet
Author: User

Mediafile. Java

Package COM. jaycee. vplayer. util; import Java. util. hashmap; import Java. util. iterator;/*** determine the file type * mediascanner helper class. */public class mediafile {// comma separated list of all file extensions supported by the media category public static string sfileextensions; // audio file types public static final int file_type_mp3 = 1; public static final int file_type_m4a = 2; public static final int fil E_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 file types public static final int file_type_mid = 11; public static final in T 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 sta TIC final int file_type_wmv = 25; Private Static final int first_video_file_type = file_type_mp4; Private Static final int compression = file_type_wmv; // 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 = 3 5; Private Static final int first_image_file_type = file_type_jpeg; Private Static final int types = file_type_wbmp; // 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; Private Static final int first_playlist_file_type = file_type_m3u; Private Static final int last_playlist_file_ty Pe = file_type_wpl; // static internal class 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, mediafiletype> (); Private Static hashmap <string, integer> smimetypemap = new hashmap <string, integer> (); static void addfiletype (String extension, int filetype, string mimetype) {sfiletypemap. put (extension, new mediafiletype (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"); addfil Etype ("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, "applic Ation/vnd. MS-WPL "); // compute file extensions list for native Media extends 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>"; Publ IC static Boolean isaudiofiletype (INT filetype) {return (filetype >=first_audio_file_type & filetype <= enabled) | (filetype >=first_midi_file_type & filetype <= enabled ));} public static Boolean isvideofiletype (INT filetype) {return (filetype> = first_video_file_type & filetype <= last_video_file_type);} public static Boolean isimagefiletype (INT filetype) {RET Urn (filetype >=first_image_file_type & filetype <= last_image_file_type);} public static Boolean isplaylistfiletype (INT filetype) {return (filetype >=struct & filetype <= struct );} public static mediafiletype getfiletype (string path) {int lastdot = path. lastindexof (". "); If (lastdot <0) return NULL; return sfiletypemap. get (path. substring (lastdot + 1 ). to Uppercase ();} // determine the file type public static Boolean isvideofiletype (string path) based on the video file path {// Add mediafiletype = getfiletype (PATH); If (null! = Type) {return isvideofiletype (type. filetype);} return false;} // determine the file type based on the audio file path. Public static Boolean isaudiofiletype (string path) {// Add mediafiletype = getfiletype (PATH ); if (null! = Type) {return isaudiofiletype (type. filetype);} return false;} // view the file type according to the MIME type public static int getfiletypeformimetype (string mimetype) {integer value = smimetypemap. get (mimetype); Return (value = NULL? 0: value. intvalue ());}}

 

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.