Background introduction:
MIME: Full name Multipurpose Internet Mail Extensions, multi-function Internet Message expansion service. It is a Multipurpose Internet Mail Expansion protocol that was first applied to the e-mail system in 1992, but was later applied to the browser. A MIME type is a type of file that sets an extension that is opened by an application, and the browser automatically opens with the specified application when the extension file is accessed. Many are used to specify some client-customized file names, as well as some ways to open media files.
In Android, the MIME type of the file is used to determine which applications can process the files and use one of the applications (if there are multiple optional applications, the user must specify one) to handle.
When I write the Android explorer (file browser), I want to be able to implement the open file in the Explorer, then I need to use the MIME type of the file.
----------------------------------------Background Split Line-----------------------------------------
Implementation method:
/**
* 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 ();
Gets 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 matching table of mime and file types.
for (int i=0;i<mime_maptable.length;i++) {
if (End.equals (mime_maptable[i][0]))
Type = mime_maptable[i][1];
}
return type;
}
/**
* Open File
* @param file
*/
private void OpenFile (file file) {
Uri uri = uri.parse ("file://" +file.getabsolutepath ());
Intent Intent = new Intent ();
Intent.addflags (Intent.flag_activity_new_task);
Set the Action property of intent
Intent.setaction (Intent.action_view);
Gets the MIME type of file files
String type = getmimetype (file);
Sets the data and type properties of the intent.
Intent.setdataandtype (/*uri*/uri.fromfile (file), type);
Jump
StartActivity (Intent);
}
Now there is a matching table for MIME type and file type.
Match table for "file type--mime type":
Create a matching table of MIME type and file suffix name
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"},
{". 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"},
{". Prop", "Text/plain"},
{". rar", "application/x-rar-compressed"},
{". rc", "Text/plain"},
{". rmvb", "Audio/x-pn-realaudio"},
{". rtf", "Application/rtf"},
{". Sh", "Text/plain"},
{". Tar", "Application/x-tar"},
{". tgz",
{". txt", "Text/plain"},
{". wav", "Audio/x-wav"},
{". wma", "audio/x-ms-wma"},
{". wmv", "audio/x-ms-wmv"},
{". wps", "Application/vnd.ms-works"},
{". xml", "Text/xml"},
{". xml", "Text/plain"},
{". Z", "application/x-compress"},
{". zip", "Application/zip"},
{"", "*/*"}
};
This table is not yet complete, the above is just some of the common file types, for other file types and mime matching values I will be updated in the future.
Match table for file type and mime in Android (GO)