This article explains the use of the Android file selector. It is actually getting the path of the file or folder that the user selected in the SD card, much like the OpenFileDialog control in C #.
The implementation of this instance is simple, so that you can quickly familiarize yourself with the Android file selector and improve development efficiency.
On the internet has seen a case about the file selector, many people have seen, this example is modified by it, but easier to understand, more efficient, in addition, this example has its own characteristics:
1, listening to the user to press the back key event, so that it returned to the previous level of the directory.
2. Special treatment is made for different file types (file vs folder, target file vs other file).
Knowledge point One, use of the File class
The main features of the file selector are: Browse files \ folders, file types, etc., all through the Java file class.
Knowledge point two, call method description
Use the Startactivityforresult () initiate call and the Onactivityresult () method to receive the information after the callback.
First put the effect of the picture is as follows:
The other is nothing to say, let's look at the code comments, very simple.
Filechooseractivity.java the class that implements the file selection.
Java code
public class Copyoffilechooseractivity extends activity {private String msdcardrootpath;//sdcard Root Path private String Mlastfilepath;
The path currently displayed private arraylist<fileinfo> mfilelists;
Private Filechooseradapter Madatper;
Configure the adapter private void Setgridviewadapter (String filePath) {updatefileitems (FilePath);
Madatper = new Filechooseradapter (this, mfilelists);
Mgridview.setadapter (Madatper);
///Update the data according to the path and notify the Adatper data to change the private void Updatefileitems (String filePath) {mlastfilepath = FilePath;
Mtvpath.settext (Mlastfilepath);
if (mfilelists = = null) mfilelists = new arraylist<fileinfo> ();
if (!mfilelists.isempty ()) mfilelists.clear ();
file[] files = Folderscan (FilePath);
if (files = null) return;
for (int i = 0; i < files.length i++) {if (Files[i].ishidden ())//Do not show hidden file continue; String Fileabsolutepath = files[i].GetAbsolutePath ();
String fileName = Files[i].getname ();
Boolean isdirectory = false;
if (Files[i].isdirectory ()) {isdirectory = true;
} FileInfo FileInfo = new FileInfo (Fileabsolutepath, FileName, isdirectory);
Add to List Mfilelists.add (FileInfo); //when the object of Madatper don ' t initialized if (madatper!= null) Madatper.notifydatas Etchanged ();
Re-refresh}//Get all files of the current path private file[] Folderscan (String path) {File File = new file (path);
file[] files = file.listfiles ();
return files; Private Adapterview.onitemclicklistener Mitemclicklistener = new Onitemclicklistener () {public void Onitemclick (adapterview<?> Adapterview, view view, int position, long ID)
{FileInfo FileInfo = (FileInfo) ((Filechooseradapter) Adapterview.getadapter ()). GetItem (position)); if (Fileinfo.isdirectory ())//Click the item as a folder, display all files under the folder UpdatefileItems (Fileinfo.getfilepath ());
else if (Fileinfo.ispptfile ()) {//is a PPT file, the path is notified to the caller Intent Intent = new Intent ();
Intent.putextra (Extra_file_chooser, Fileinfo.getfilepath ());
Setresult (RESULT_OK, intent);
Finish ();
} else {//other files ... toast (GetText (R.string.open_file_error_format));
}
}
}; public boolean onKeyDown (int keycode, keyevent event) {if (event.getaction () = = Keyevent.action_down && Event
. getKeyCode () = = Keyevent.keycode_back) {backprocess ();
return true;
Return Super.onkeydown (KeyCode, event);
//Returns the operation of the previous directory The public void Backprocess () {//Determines whether the current path is a sdcard path, and if not, goes back to the previous layer.
if (!mlastfilepath.equals (Msdcardrootpath)) {File Thisfile = new file (Mlastfilepath);
String Parentfilepath = Thisfile.getparent ();
Updatefileitems (Parentfilepath); else {//is the sdcard path, directly ending Setresult (result_canceLED);
Finish ();
}
}
}
The interface of this instance is slightly shabby, but you can build on it and add other features. This instance code downloads the address:
http://download.csdn.net/detail/qinjuning/4825392.
Thank you for reading, I hope to help you, thank you for your support for this site!