Note-android open files in various formats (APK, Word, Excel, PPT, PDF, audio and video, pictures, etc.)

Source: Internet
Author: User

Open the suffix. apk file, that is, start the installer;

Apkfilepath file path public void installapk (String apkfilepath) {           //create URI uri uri        = uri.fromfile (new file Apkfilepath));        Intent Intent = new Intent (intent.action_view);        Set the URI and type        intent.setdataandtype (URI, "application/vnd.android.package-archive");        Perform installation        mcontext.startactivity (intent);    }   
/** * Open multiple types of files * @param path file paths * @param type file type */public void OpenText (String path, int type) {Intent Intent =             New Intent (Intent.action_view);             Intent.addcategory (Intent.category_default);             Intent.addflags (Intent.flag_activity_new_task);         Uri uri = uri.fromfile (new File);          Determine the file type if (file_type_ppt = = type) {Intent.setdataandtype (URI, "Application/vnd.ms-powerpoint");             } else if (File_type_word = = TYPE) {intent.setdataandtype (URI, "Application/msword");             } else if (File_type_excel = = TYPE) {intent.setdataandtype (URI, "application/vnd.ms-excel");             } else if (file_type_txt = = TYPE) {intent.setdataandtype (URI, "Text/plain");             } else if (file_type_pdf = = TYPE) {intent.setdataandtype (URI, "application/pdf"); } else if (file_type_html = = TYPE) {Uri Htmluri = uri.parse (path). Buildupon (). encodedauthority ("com. Android.htmlfileprovider "). Scheme (" Content "). Encodedpath (Path). build ();         Intent.setdataandtype (Htmluri, "text/html");        } try {activity.startactivity (intent);        } catch (Exception e) {toast.maketext (Mcontext, "The program that supports this format is not installed in the device", Toast.length_short). Show (); }}
<pre name= "code" class= "Java" >////////open multimedia type Intent.setdataandtype (URI, "audio/*");  Audio Intent.setdataandtype (URI, "video/*");  Video Intent.setdataandtype (URI, "image/*");  Picture Intent.setdataandtype (URI, "Application/x-chm");     Open CHM File

Determines whether the file name is a suffix of some kind private Boolean check (final String name, Final string[] extensions) {for (string end:extensions) {if (Name.tolowercase (). EndsWith (end)) {return true;}} return false;} Set the type   if (check (name, ". apk")) {     file.settype (file_type_apk);    } else  if (check (name, ". PDF ")) {         file.settype (file_type_pdf);    } else if (check (name,             Getstringarray (r.array.ppt_filter))) {         File.settype (file_type_ppt);    } ..... array.ppt_filter:<array name= "Ppt_filter" >        <item>.ppt</item> <------        Item>.pptx</item>    </array>


Get device Storage path (SD card, USB)
Environment.getexternalstoragedirectory ();

Environment.getexternalstoragedirectory (). GetParent ();

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.