In applications, how to call the software installed in the system to open a file is a common problem. Below is a method I have used. Let's share it with you!
This is a method to open a file:
Java code /**
* Open a file
* @ Param file
*/
Private void openFile (File file ){
Intent intent = new Intent ();
Intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
// Set the intent Action attribute
Intent. setAction (Intent. ACTION_VIEW );
// Obtain the MIME type of the file
String type = getMIMEType (file );
// Set the data and Type attributes of intent.
Intent. setDataAndType (/* uri */Uri. fromFile (file), type );
// Jump
StartActivity (intent );
}
/**
* Obtain the corresponding MIME type based on the file suffix.
* @ Param file
*/
Private String getMIMEType (File file ){
String type = "*/*";
String fName = file. getName ();
// Obtain the position of the separator "." Before the Suffix in fName.
Int dotIndex = fName. lastIndexOf (".");
If (dotIndex <0 ){
Return type;
}
/* Get the file suffix */
String end = fName. substring (dotIndex, fName. length (). toLowerCase ();
If (end = "") return type;
// Find the corresponding MIME type in the MIME-object type matching table.
For (int I = 0; I <MIME_MapTable.length; I ++) {// MIME_MapTable ?? Here you must have a question: what is the MIME_MapTable?
If (end. equals (MIME_MapTable [I] [0])
Type = MIME_MapTable [I] [1];
}
Return type;
}
MIME_MapTable is a String array of the MIME type corresponding to the suffix of all files:
Java code <span style = "white-space: pre;"> </span> private final String [] [] MIME_MapTable = {
// {Suffix, 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.doc ument "},
{". 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 want to add it?
From chenghai2011