Android is used to open intent of various files)

Source: Internet
Author: User

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;

// Customize the android intent class,

// You can obtain the intent used to open the following files.

// PDF, PPT, Word, Excel, CHM, HTML, text, audio, and video

Example:

// This token can be used for example. APKProgramYou are not authorized to access the asset resource files in other APK files, or is the path incorrect?

 // Intent it = getpdffileintent ("file: // android_asset/helphelp ");

 // The following are all OK

 // Intent it = gethtmlfileintent ("/mnt/sdcard/tutorial.html"); // SD card home directory

// Intent it = gethtmlfileintent ("/sdcard/tutorial.html"); // SD card Home Directory, which can also be

 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/001020.80.jpg ");

 // Intent it = gettextfileintent ("/mnt/sdcard/hello.txt", false );

 Startactivity (it );

Public class myintent

{

// Android obtains an intent used to open an HTML file.

Public static intent gethtmlfileintent (string PARAM)

{

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 used to open an image file.

Public static intent getimagefileintent (string PARAM)

{

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 used to open a PDF file.

Public static intent getpdffileintent (string PARAM)

{

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 used to open a text file.

Public static intent gettextfileintent (string Param, Boolean paramboolean)

 

 

 {

 

 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 used to open the audio file.

Public static intent getaudiofileintent (string PARAM)

{

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 used to open a video file.

Public static intent getvideofileintent (string PARAM)

{

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 used to open the CHM File.

Public static intent getchmfileintent (string PARAM)

{

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 used to open a Word file.

Public static intent getwordfileintent (string PARAM)

{

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 used to open an Excel file.

Public static intent getexcelfileintent (string PARAM)

{

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 used to open the pptfile.

Public static intent getpptfileintent (string PARAM)

{

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;

}

}

Related Article

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.