Android open file intent and usage

Source: Internet
Author: User
From http://my.oschina.net/yuhanxun/blog/81007

When writing a file management system, you need to open files of different formats. Because Android systems have built-in system applications that can be opened by default, they still cannot meet the requirements, for example, to open a video file or word, you need to install the corresponding playback software to use it. Then, the program will use intent to find the software that can be used.

To open a file through code, two parts are required. One part is to obtain the suffixes of different files to match the corresponding intent as needed, and the other is to open different intent files in different formats.

1. Define the filename extension array file fileendings under the values directory

<?xml version="1.0" encoding="utf-8"?><resources>    <array name="fileEndingImage">        <item>.png</item>        <item>.gif</item>        <item>.jpg</item>        <item>.jpeg</item>        <item>.bmp</item>    </array>    <array name="fileEndingAudio">        <item>.mp3</item>        <item>.wav</item>        <item>.ogg</item>        <item>.midi</item>    </array>    <array name="fileEndingVideo">        <item>.mp4</item>        <item>.rmvb</item>        <item>.avi</item>        <item>.flv</item>    </array>    <array name="fileEndingPackage">        <item>.jar</item>        <item>.zip</item>        <item>.rar</item>        <item>.gz</item>        <item>.apk</item>        <item>.img</item>    </array>    <array name="fileEndingWebText">        <item>.htm</item>        <item>.html</item>        <item>.php</item>        <item>.jsp</item>    </array>    <array name="fileEndingText">        <item>.txt</item>        <item>.java</item>        <item>.c</item>        <item>.cpp</item>        <item>.py</item>        <item>.xml</item>        <item>.json</item>        <item>.log</item>    </array>    <array name="fileEndingWord">        <item>.doc</item>        <item>.docx</item>    </array>    <array name="fileEndingExcel">        <item>.xls</item>        <item>.xlsx</item>    </array>    <array name="fileEndingPPT">        <item>.ppt</item>        <item>.pptx</item>    </array>    <array name="fileEndingPdf">        <item>.pdf</item>    </array></resources>

2. Define the openfiles tool class. You only need to transmit the file parameter, and then open the file through the returned intent

Public class openfiles {// Android obtains an intent public static intent gethtmlfileintent (File file) {URI uri = Uri. parse (file. tostring ()). buildupon (). encodedauthority ("com.android.html fileprovider "). scheme ("content "). encodedpath (file. tostring ()). build (); intent = new intent ("android. intent. action. view "); intent. setdataandtype (Uri, "text/html"); Return intent;} // Android obtains an intent public static intent getimagefileintent (File file) used to open an image file) {intent = new intent ("android. intent. action. view "); intent. addcategory ("android. intent. category. default "); intent. addflags (intent. flag_activity_new_task); Uri uri = Uri. fromfile (File); intent. setdataandtype (Uri, "image/*"); Return intent;} // Android obtains an intent public static intent getpdffileintent (File file) used to open a PDF file) {intent = new intent ("android. intent. action. view "); intent. addcategory ("android. intent. category. default "); intent. addflags (intent. flag_activity_new_task); Uri uri = Uri. fromfile (File); intent. setdataandtype (Uri, "application/pdf"); Return intent;} // Android obtains an intent public static intent gettextfileintent (File file) used to open a text file) {intent = new intent ("android. intent. action. view "); intent. addcategory ("android. intent. category. default "); intent. addflags (intent. flag_activity_new_task); Uri uri = Uri. fromfile (File); intent. setdataandtype (Uri, "text/plain"); Return intent;} // Android obtains an intent public static intent getaudiofileintent (File file) used to open the audio file) {intent = new intent ("android. intent. action. view "); intent. addflags (intent. flag_activity_clear_top); intent. putextra ("oneshot", 0); intent. putextra ("configchange", 0); Uri uri = Uri. fromfile (File); intent. setdataandtype (Uri, "audio/*"); Return intent;} // Android obtains an intent public static intent getvideofileintent (File file) used to open the video file) {intent = new intent ("android. intent. action. view "); intent. addflags (intent. flag_activity_clear_top); intent. putextra ("oneshot", 0); intent. putextra ("configchange", 0); Uri uri = Uri. fromfile (File); intent. setdataandtype (Uri, "Video/*"); Return intent;} // Android obtains an intent public static intent getchmfileintent (File file) used to open the CHM File) {intent = new intent ("android. intent. action. view "); intent. addcategory ("android. intent. category. default "); intent. addflags (intent. flag_activity_new_task); Uri uri = Uri. fromfile (File); intent. setdataandtype (Uri, "application/X-CHM"); Return intent;} // Android obtains an intent public static intent getwordfileintent (File file) used to open a Word file) {intent = new intent ("android. intent. action. view "); intent. addcategory ("android. intent. category. default "); intent. addflags (intent. flag_activity_new_task); Uri uri = Uri. fromfile (File); intent. setdataandtype (Uri, "application/MSWord"); Return intent;} // Android obtains an intent public static intent getexcelfileintent (File file) used to open an Excel file) {intent = new intent ("android. intent. action. view "); intent. addcategory ("android. intent. category. default "); intent. addflags (intent. flag_activity_new_task); Uri uri = Uri. fromfile (File); intent. setdataandtype (Uri, "application/vnd. MS-excel "); Return intent;} // Android obtains an intent public static intent getpptfileintent (File file) {intent = new intent (" android. intent. action. view "); intent. addcategory ("android. intent. category. default "); intent. addflags (intent. flag_activity_new_task); Uri uri = Uri. fromfile (File); intent. setdataandtype (Uri, "application/vnd. MS-PowerPoint "); Return intent;} // Android obtains an intent public static intent getapkfileintent (File file) {intent = new intent (); intent. addflags (intent. flag_activity_new_task); intent. setaction (Android. content. intent. action_view); intent. setdataandtype (URI. fromfile (file), "application/vnd. android. package-Archive "); Return intent ;}}

3. Define the suffix used to check whether the file to be opened is in the traversal suffix Array


private boolean checkEndsWithInStringArray(String checkItsEnd,                     String[] fileEndings){        for(String aEnd : fileEndings){            if(checkItsEnd.endsWith(aEnd))                return true;        }        return false;    }

4. Open the corresponding file by calling the intent returned by the openfiles class.


If (currentpath! = NULL & effectpath. isfile () {string filename = currentpath. tostring (); intent; If (checkendswithinstringarray (filename, getresources (). getstringarray (R. array. fileendingimage) {intent = openfiles. getimagefileintent (currentpath); startactivity (intent);} else if (checkendswithinstringarray (filename, getresources (). getstringarray (R. array. fileendingwebtext) {intent = openfiles. gethtmlfileintent (C Urrentpath); startactivity (intent);} else if (checkendswithinstringarray (filename, getresources (). getstringarray (R. array. fileendingpackage) {intent = openfiles. getapkfileintent (currentpath); startactivity (intent);} else if (checkendswithinstringarray (filename, getresources (). getstringarray (R. array. fileendingaudio) {intent = openfiles. getaudiofileintent (currentpath); startactivity (intent);} els E if (checkendswithinstringarray (filename, getresources (). getstringarray (R. array. fileendingvideo) {intent = openfiles. getvideofileintent (currentpath); startactivity (intent);} else if (checkendswithinstringarray (filename, getresources (). getstringarray (R. array. fileendingtext) {intent = openfiles. gettextfileintent (currentpath); startactivity (intent);} else if (checkendswithinstringarray (filename, Getresources (). getstringarray (R. array. fileendingpdf) {intent = openfiles. getpdffileintent (currentpath); startactivity (intent);} else if (checkendswithinstringarray (filename, getresources (). getstringarray (R. array. fileendingword) {intent = openfiles. getwordfileintent (currentpath); startactivity (intent);} else if (checkendswithinstringarray (filename, getresources (). getstringarray (R. array. fileend Ingexcel) {intent = openfiles. getexcelfileintent (currentpath); startactivity (intent);} else if (checkendswithinstringarray (filename, getresources (). getstringarray (R. array. fileendingppt) {intent = openfiles. getpptfileintent (currentpath); startactivity (intent);} else {showmessage ("cannot be opened. Please install the appropriate software! ") ;}} Else {showmessage (" sorry, this is not a 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.