Android opens different types of files

Source: Internet
Author: User

Transfer from http://www.cnblogs.com/hibraincol/archive/2010/09/16/1828502.html

Just do a download preview function, to open the file, see this very good article turned around. The OpenFile method can be changed by itself by Intent.createchooser () mode to select the open program.

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:

/** * Gets the corresponding MIME type 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;    }/* Gets 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 Files * @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 the intent Intent.setaction (Intent.action_view);    Gets the MIME type of file, 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"}, {". A    PK "," 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/MSW    Ord "}, {". exe "," Application/octet-stream "}, {". gif "," Image/gif "}, {". Gtar "," Application/x-gtar "}, {". Gz", "Application/x-gzip"}, {". h", "Text/plain"}, {". htm", "text/html"}, {". html", "text/h Tml "}, {". 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.mpeg    URL "}, {". 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", "applic        Ation/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 "," application/x-compressed "}, {". txt "," Text/plain "}, {". wav "," Audio/x-wa V "}, {". 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", "Applica Tion/zip "}, {" "," */* "}};

Android opens different types of files

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.