Android Open Local File

Source: Internet
Author: User
Tags gtar

Android opens a local file, which is actually very common. And now there's a mobile version of Office. It's even easier to see the whole family of office buckets.

The first thing to know is that the Android open local file is opened according to the type, that is, based on the MIME type of the file to determine

If you don't know what the type is, it is: */*

Type Match table:

private static 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 "," Applicatio        N/octet-stream "}, {". gif "," Image/gif "}, {". Gtar "," Application/x-gtar "}, {". Gz ", "Application/x-gzip"}, {". h", "Text/plain"}, {". htm", "text/html"}, {". HT            ML "," 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.certificat            E "}, {". Mpe "," Video/mpeg "}, {". mpeg "," Video/mpeg "}, {". mpg "," Video/mpeg "}, {". Mpg4", "Video/mp4"}, {". MPGA", "Audio/mpeg"}, {". Msg", "Application/vnd.ms-outl Ook "}, {". 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 "}, {". rm            vb "," Audio/x-pn-realaudio "}, {". rtf "," Application/rtf "}, {". Sh "," Text/plain "}, {". Tar", "Application/x-tar"}, {". tgz", "application/x-compressed"}, {". txt", "text/p Lain "}, {". wav "," Audio/x-wav "}, {". wma "," audio/x-ms-wma "}, {". wmv "," audio/x-m    S-wmv "}, {". wps "," Application/vnd.ms-works "},//{". xml "," Text/xml "}, {". xml ", "Text/plain"}, {". Z", "application/x-compress"}, {". zip", "Application/zip"}, { "",        "*/*"}    };

Gets the type of the 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 fileType = fname.substring (Dotindex,fname.length ()). toLowerCase ();        if (FileType = = NULL | | ". Equals (FileType))            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 (filetype.equals (mime_maptable[i][0]))                type = Mime_ MAPTABLE[I][1];        }        return type;    }

Open File

    Intent Intent = new Intent ();    File File = new file (filedirectory);    Intent.addflags (intent.flag_activity_new_task);//Set Tag    intent.setaction (Intent.action_view);//action, view    Intent.setdataandtype (uri.fromfile (file), GetMimeType (file));//set Type    context.startactivity (intent);

  

Android Open Local File

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.