Android calls system installed software to open file (go)
In the application how to call the system installed software to open a file, this is the problem we often encounter, the following is a way I used to share with you!
This is a way to open the file:
Java code
- /**
- * Open File
- * @param file
- */
- Private void OpenFile (file file) {
- Intent Intent = new Intent ();
- Intent.addflags (Intent.flag_activity_new_task);
- //Set the Action property of Intent
- Intent.setaction (Intent.action_view);
- //Get MIME type of file
- String type = getmimetype (file);
- //Set the data and type properties of the intent.
- Intent.setdataandtype (/*uri*/uri.fromfile (file), type);
- //Jump
- StartActivity (Intent);
- }
- /**
- * The corresponding MIME type is obtained according to the file suffix name.
- * @param file
- */
- Private String getmimetype (file file) {
- String type="*/*";
- String fName = File.getname ();
- //Get the delimiter "." Before the suffix name. The position in the fname.
- int dotindex = Fname.lastindexof (".");
- if (Dotindex < 0) {
- return type;
- }
- /* Get the suffix name of the file */
- String end=fname.substring (Dotindex,fname.length ()). toLowerCase ();
- if (end=="")return type;
- //The corresponding MIME type is found in the MIME and file type match table.
- For (int i=0;i<mime_maptable.length;i++) { //mime_maptable?? You must have a question here, what is this mime_maptable?
- if (end.equals (mime_maptable[i][0]))
- Type = mime_maptable[i][1];
- }
- return type;
- }
Mime_maptable is a string array of MIME types that correspond to the suffix names of all files:
Java code
- Private final string[][] mime_maptable={
- //{suffix name, MIME type}
- {". 3gp", "VIDEO/3GPP"},
- {". apk", "Application/vnd.android.package-archive"},
- {". asf", "video/x-ms-asf"},
- {". avi", "Video/x-msvideo"},
- {". Bin", "Application/octet-stream"},
- {". bmp", "Image/bmp"},
- {". C", "Text/plain"},
- {". Class", "Application/octet-stream"},
- {". conf", "Text/plain"},
- {". cpp", "Text/plain"},
- {". doc", "Application/msword"},
- {". docx", "Application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
- {". xls", "application/vnd.ms-excel"},
- {". xlsx", "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
- {". exe", "Application/octet-stream"},
- {". gif", "Image/gif"},
- {". Gtar", "Application/x-gtar"},
- {". Gz", "Application/x-gzip"},
- {". h", "Text/plain"},
- {". htm", "text/html"},
- {". html", "text/html"},
- {". Jar", "Application/java-archive"},
- {". Java", "Text/plain"},
- {". jpeg", "Image/jpeg"},
- {". jpg", "Image/jpeg"},
- {". js", "Application/x-javascript"},
- {". Log", "Text/plain"},
- {". m3u", "Audio/x-mpegurl"},
- {". m4a", "Audio/mp4a-latm"},
- {". m4b", "Audio/mp4a-latm"},
- {". m4p", "Audio/mp4a-latm"},
- {". M4u", "Video/vnd.mpegurl"},
- {". m4v", "video/x-m4v"},
- {". mov", "Video/quicktime"},
- {". Mp2", "Audio/x-mpeg"},
- {". mp3", "Audio/x-mpeg"},
- {". mp4", "Video/mp4"},
- {". MPC", "Application/vnd.mpohun.certificate"},
- {". Mpe", "Video/mpeg"},
- {". Mpeg", "Video/mpeg"},
- {". mpg", "Video/mpeg"},
- {". Mpg4", "Video/mp4"},
- {". MPGA", "Audio/mpeg"},
- {". Msg", "Application/vnd.ms-outlook"},
- {". ogg", "Audio/ogg"},
- {". pdf", "Application/pdf"},
- {". png", "Image/png"},
- {". pps", "Application/vnd.ms-powerpoint"},
- {". ppt", "Application/vnd.ms-powerpoint"},
- {". pptx", "Application/vnd.openxmlformats-officedocument.presentationml.presentation"},
- {". Prop", "Text/plain"},
- {". rc", "Text/plain"},
- {". rmvb", "Audio/x-pn-realaudio"},
- {". rtf", "Application/rtf"},
- {". Sh", "Text/plain"},
- {". Tar", "Application/x-tar"},
- {". tgz", "application/x-compressed"},
- {". txt", "Text/plain"},
- {". wav", "Audio/x-wav"},
- {". wma", "Audio/x-ms-wma"},
- {". wmv", "audio/x-ms-wmv"},
- {". WPs", "Application/vnd.ms-works"},
- {". xml", "Text/plain"},
- {". Z", "Application/x-compress"},
- {". zip", "application/x-zip-compressed"},
- {"", "*/*"}
- };
This MIME type may not be complete, do you have anything to add?
Original: http://tonysun3544.iteye.com/blog/1265884
Android calls system installed software to open file (go)