Android implement intent method to open various files _android

Source: Internet
Author: User
Tags sqlite database

This article is an example of the Android implementation of the intent method to open various files. Share to everyone for your reference, specific as follows:

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 get intent that open the following file
//pdf,ppt,word,excel,chm,html,text,audio,video

Example:

This is not possible because the PDF.APK program does not have permission to access asset resource files in other apk, or is the path incorrectly written?
Intent it = getpdffileintent ("File:///android_asset/helphelp.pdf"); The following are 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 Intent it = gethtmlfileintent ("/system/etc/tutorial.html ");//etc directory within the System//
Intent it = getpdffileintent ("/system/etc/helphelp.pdf");
Intent it = getwordfileintent ("/system/etc/help.doc"); Intent it = getexcelfileintent ("/mnt/sdcard/book1.xls")//intent it = getpptfileintent ("/mnt/sdcard/download/
Android_ppt.ppt ");//intent it = getvideofileintent ("/mnt/sdcard/ice.avi ") under the download directory of the//SD card;
Intent it = getaudiofileintent ("/mnt/sdcard/ren.mp3");
Intent it = getimagefileintent ("/mnt/sdcard/images/001041580.jpg");
Intent it = gettextfileintent ("/mnt/sdcard/hello.txt", false);
StartActivity (IT); public class Myintent {//android gets a Intent public static Intent gethtmlfileintent (String param) for opening HTML files {UrI uri = Uri.parse (param). Buildupon (). Encodedauthority ("Com.android.htmlfileprovider"). Scheme ("content").
  Encodedpath (param). build ();
  Intent Intent = new Intent ("Android.intent.action.VIEW");
  Intent.setdataandtype (URI, "text/html");
 return intent; //android get a Intent public static Intent getimagefileintent to open a picture file (String param) {Intent Intent = new Intent ("a
  Ndroid.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 get a Intent public static Intent getpdffileintent to open a PDF file (String param) {Intent Intent = new Intent ("a
  Ndroid.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 gets aIntent public static Intent gettextfileintent for opening text files (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 get a Intent public static Intent getaudiofileintent for opening audio files (String param) {Intent Intent = new Intent ("a
  Ndroid.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 gets a Intent public static Intent getvideofileintent for opening video files (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 gets a Intent public static Intent getchmfileintent for opening a CHM file (String param) {Intent Intent = new Intent ("a
  Ndroid.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 gets a Intent public static Intent getwordfileintent for opening a Word file (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 gets a inten to open an Excel fileT 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 gets a Intent public static Intent getpptfileintent for opening a ppt file (String param) {Intent Intent = new Intent ("an
  Droid.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;

 }
}

More interested readers of Android related content can view the site: "Summary of the use of Android controls," "The Summary of Android view tips," "Android File Operation tips Summary", "Android operation SQLite Database Skills Summary", " Android operations JSON format Data tips summary, "Android Database Operation skills Summary", "Android programming activity Operation Skills Summary", "Android programming development of SD card operation method Summary", "Android Development and advanced tutorial And the Android resource Operation Skills Summary

I hope this article will help you with the Android program.

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.