The intent and use method of open file for Android programming development _android

Source: Internet
Author: User
Tags rar

This article illustrates the intent and usage of the open file of Android programming development. Share to everyone for your reference, specific as follows:

In writing file management system will be used to open a variety of different formats of the file requirements, because the Android system by default built-in some open system applications, but still can not meet the requirements, such as the opening of video files, word, etc., need to install the appropriate playback software to use, In this case, the program will find the software through intent to open a file through the code requires 2 parts, in part to obtain a different file suffix, in order to match the corresponding intent according to the requirements, and the other is different format File open intent different

1, in the values directory to define the suffix array file fileendings

<?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> & lt;item>.zip</item> <item>.rar</item> <item>.gz</item> <item>.apk</item > <item>.img</item> </array> <array name= "Fileendingwebtext" > <item>.htm</item& 
Gt <item>.html</item> <item>.php</item> <item>.jsp</item> </array> <array name= "Fileendingtext" > <item>.txt</item& 
Gt <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>.p<?xml version= "1.0" encoding= "Utf-8"?> <resources> < Array name= "Fileendingimage" > <item>.png</item> <item>.gif</item> <item>.jpg</ Item> <item>.jpeg</item> &LT;ITEM&GT;.BMP&Lt;/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> & lt;item>.zip</item> <item>.rar</item> <item>.gz</item> <item>.apk</item > <item>.img</item> </array> <array name= "Fileendingwebtext" > <item>.htm</item& 
Gt <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>df</item> </array> </resources>

2, define the Openfiles tool class, simply transfer the file parameter, and then open it by returning intent

public class OpenFiles {//android gets a Intent public static Intent gethtmlfileintent (file file) {URI URI for opening an HTML file = Uri.parse (File.tostring ()). Buildupon (). Encodedauthority ("Com.android.htmlfileprovider"). Scheme ("content"). 
Encodedpath (File.tostring ()). build (); 
Intent Intent = new Intent ("Android.intent.action.VIEW"); 
Intent.setdataandtype (URI, "text/html"); 
return intent; //android gets a Intent public static Intent getimagefileintent for opening a picture file (file file) {Intent Intent = new Intent ("Androi 
D.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 gets a Intent public static Intent getpdffileintent for opening a PDF file (file 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 (fiLe); 
Intent.setdataandtype (URI, "application/pdf"); 
return intent; //android gets a Intent public static Intent gettextfileintent for opening a text file (file 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 (file); 
Intent.setdataandtype (URI, "Text/plain"); 
return intent; //android gets a Intent public static Intent getaudiofileintent for opening audio files (file file) {Intent Intent = new Intent ("Androi 
D.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 gets a Intent public static Intent getvideofileintent for opening video files (file file) {Intent Intent = new Intent ("Androi 
D.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 gets a Intent public static Intent getchmfileintent for opening a CHM file (file 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 (file); 
Intent.setdataandtype (URI, "Application/x-chm"); 
return intent; //android gets a Intent public static Intent getwordfileintent for opening a Word file (file file) {Intent Intent = new Intent ("Andro 
Id.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 gets a Intent public static Intent getexcelfileintent for opening an Excel file (file file) {Intent Intent = new Intent ("" and 
Roid.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 gets a Intent public static Intent getpptfileintent for opening a PPT file (file 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 (file); 
Intent.setdataandtype (URI, "Application/vnd.ms-powerpoint"); 
return intent; 
//android gets a Intent public static Intent getapkfileintent (file file) {Intent Intent = new Intent () for opening apk files; 
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. Defines whether the suffix used to check 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&¤tpath.isfile ()) {String fileName = currentpath.tostring (); 
Intent 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 (Currentpath); 
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); 
}else 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.fileendingexcel))) {intent = Openfiles.getexcelfileintent (Currentpath); 
StartActivity (Intent); 
}else if (Checkendswithinstringarray (FileName, Getresources (). 
Getstringarray (r.array.fileendingppt))) {intent = Openfiles.getpptfileintent (Currentpath); 
StartActivity (Intent); }else {showmessage ("unable to open), please install the appropriate software!" 
"); }}else {showmessage ("Sorry, this is not a file! 
"); 

 }

I hope this article will help you with your Android programming.

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.