Android-open various files with intent

Source: Internet
Author: User

Android-open various files with intent

Import android. app. activity; import android. content. intent; import android.net. uri; import android.net. uri. builder; import java. io. file; import android. content. intent; // custom android Intent class, // can be used to obtain the intent of the following files: // PDF, PPT, WORD, EXCEL, CHM, HTML, TEXT, AUDIO, VIDEO // error example: // can this example be caused by the fact that the. APK program does not have the permission to access the asset resource file in other APK files, or is the path written incorrectly? // Intent it = getPdfFileIntent ("file: // android_asset/helphelp;"); // all of the following are OKIntent it = getHtmlFileIntent ("/mnt/sdcard/tutorial.html "); // SD card home directory // Intent it = getHtmlFileIntent ("/sdcard/tutorial.html"); // SD card home directory, in this way, // Intent it = getHtmlFileIntent ("/system/etc/tutorial.html "); // etc directory in the system // Intent it = getPdfFileIntent ("/system/etc/helphelp "); // Intent it = getWordFileIntent ("/system/etc/help.doc"); // Intent it = getExcelFileIntent ("/mnt/sdcard/Book1.xls ") // Intent it = getPptFileIntent ("/mnt/sdcard/download/Android_PPT.ppt "); // download directory of the SD card // Intent it = getVideoFileIntent ("/mnt/sdcard/ice. avi "); // Intent it = getAudioFileIntent ("/mnt/sdcard/rename "); // Intent it = getImageFileIntent ("/mnt/sdcard/images/001011680.jpg "); // Intent it = getTextFileIntent ("/mnt/sdcard/hello.txt", false); startActivity (it ); public class MyIntent {// android obtains an intent public static Intent getHtmlFileIntent (String param) used to open an HTML file {Uri uri = Uri. parse (param ). buildUpon (). encodedAuthority ("com.android.html fileprovider "). scheme ("content "). encodedPath (param ). build (); Intent intent = new Intent ("android. intent. action. VIEW "); intent. setDataAndType (uri, "text/html"); return intent;} // android obtains an intent public static Intent getImageFileIntent (String param) used to open an image file) {Intent intent = new Intent ("android. intent. action. VIEW "); intent. addCategory ("android. intent. category. DEFAULT "); intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri. fromFile (new File (param); intent. setDataAndType (uri, "image/*"); return intent;} // android obtains an intent public static Intent getPdfFileIntent (String param) used to open a PDF file) {Intent intent = new Intent ("android. intent. action. VIEW "); intent. addCategory ("android. intent. category. DEFAULT "); intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri. fromFile (new File (param); intent. setDataAndType (uri, "application/pdf"); return intent;} // android obtains an intent public static Intent getTextFileIntent (String param, boolean paramBoolean) used to open a text file) {Intent intent = new Intent ("android. intent. action. VIEW "); intent. addCategory ("android. intent. category. DEFAULT "); intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); if (paramBoolean) {Uri uri1 = Uri. parse (param); intent. setDataAndType (uri1, "text/plain");} else {Uri uri2 = Uri. fromFile (new File (param); intent. setDataAndType (uri2, "text/plain");} return intent;} // android obtains an intent public static Intent getAudioFileIntent (String param) used to open the audio file) {Intent 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 (new File (param); intent. setDataAndType (uri, "audio/*"); return intent;} // android obtains an intent public static Intent getVideoFileIntent (String param) used to open the video file) {Intent 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 (new File (param); intent. setDataAndType (uri, "video/*"); return intent;} // android obtains an intent public static Intent getChmFileIntent (String param) used to open the CHM File) {Intent intent = new Intent ("android. intent. action. VIEW "); intent. addCategory ("android. intent. category. DEFAULT "); intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri. fromFile (new File (param); intent. setDataAndType (uri, "application/x-chm"); return intent;} // android obtains an intent public static Intent getWordFileIntent (String param) used to open a Word file) {Intent intent = new Intent ("android. intent. action. VIEW "); intent. addCategory ("android. intent. category. DEFAULT "); intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri. fromFile (new File (param); intent. setDataAndType (uri, "application/msword"); return intent;} // android obtains an intent public static Intent getExcelFileIntent (String param) used to open an Excel file) {Intent intent = new Intent ("android. intent. action. VIEW "); intent. addCategory ("android. intent. category. DEFAULT "); intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri. fromFile (new File (param); intent. setDataAndType (uri, "application/vnd. ms-excel "); return intent;} // android obtains an intent public static Intent getPptFileIntent (String param) {Intent intent Intent = new intent (" android. intent. action. VIEW "); intent. addCategory ("android. intent. category. DEFAULT "); intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri. fromFile (new File (param); intent. setDataAndType (uri, "application/vnd. ms-powerpoint "); return intent ;}}

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.