Android open various types of file method summary

Source: Internet
Author: User

Very simply, by invoking the system's intent, we can open a variety of files, unfamiliar friends can understand the action, datatype, URI related knowledge.

The general method is as follows:

public static Intent OpenFile (String filePath) {File File = new file (FilePath), if (!file.exists ()) return null;/* get extension */st Ring End=file.getname (). substring (File.getname (). LastIndexOf (".") + 1,file.getname (). Length ()). toLowerCase (); /* depends on the type of extension mimetype */if (end.equals ("m4a") | | End.equals ("MP3") | | End.equals ("mid") | | End.equals ("XMF") | | End.equals ("ogg") | | End.equals ("wav")) {return getaudiofileintent (FilePath);} else if (end.equals ("3gp") | | End.equals ("mp4")) {return getaudiofileintent (FilePath);} else if (end.equals ("jpg") | | End.equals ("gif") | | End.equals ("png") | | End.equals ("JPEG") | | End.equals ("BMP")) {return getimagefileintent (FilePath);} else if (end.equals ("apk")) {return getapkfileintent (FilePath);} else if (end.equals ("ppt")) {return getpptfileintent (FilePath);} else if (end.equals ("xls")) {return getexcelfileintent (FilePath);} else if (End.equals ("Doc")) {return getwordfileintent (FilePath);} else if (end.equals ("PDF")) {return getpdffileintent (FilePath);} else if (end.equals ("CHM")) {return Getchmfileintent (filEpath);} else if (end.equals ("TXT")) {return gettextfileintent (filepath,false);} Else{return getallintent (FilePath);}}  Android gets a intentpublic static Intent getallintent (String param) {Intent Intent = new Intent () to open the APK file;  Intent.addflags (Intent.flag_activity_new_task);  Intent.setaction (Android.content.Intent.ACTION_VIEW); Uri uri = uri.fromfile (new File (param)); Intent.setdataandtype (URI, "*/*"); return intent;}  Android gets a intentpublic static Intent getapkfileintent (String param) {Intent Intent = new Intent () to open the APK file;  Intent.addflags (Intent.flag_activity_new_task);  Intent.setaction (Android.content.Intent.ACTION_VIEW); Uri uri = uri.fromfile (new File (param)); Intent.setdataandtype (URI, "application/vnd.android.package-archive"); return intent;} Android gets a intentpublic static Intent getvideofileintent (String param) {Intent Intent = new Intent for opening video files ("Andro Id.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 intentpublic static Intent getaudiofileintent (String param) {Intent Intent = new Intent for opening audio files (" 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 gets a Intent public static Intent gethtmlfileintent (String param) {uri uri = uri.parse (param) used to open the HTML file. Buildu Pon (). 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 gets a intentpublic static Intent getimagefileintent (String param) {Intent Intent = new Intent ("Android") to open the picture file. 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 gets a Intent public static Intent getpptfileintent (String param) {Intent Intent = new Intent for opening ppt files ("Androi   D.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; //android gets a Intent public static Intent getexcelfileintent (String param) {Intent Intent = new Intent for opening an Excel file   ("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 getwordfileintent (String param) that is used to open the 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 gets a Intent public static Intent getchmfileintent (String param) {Intent Intent = new Intent for opening CHM files ("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 gettextfileintent (String param, Boolean paramboolean) for opening a text file {Intent I   ntent = 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 gets a Intent public static Intent getpdffileintent (String param) {Intent Intent = new Intent for opening PDF files ("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/pdf");   return intent; }

Above.

Android open various types of file method summary

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.