This article explains the use of the Android file selector. is actually getting the path of the file or folder that the user chooses in the SD card, which is much like the OpenFileDialog control in C #.
The implementation of this example is very simple, so that you can quickly become familiar with the Android file selector, improve development efficiency.
Online has seen a file selector on the instance, many people have seen that this example is based on its modification, but easier to understand, more efficient, in addition, this example has its own characteristics:
1, listens to the user presses the back key event, causes it to return to the previous level directory.
2. Special handling for different file types (file vs folder, target file vs other file).
knowledge Point One, use of the File class
The main functions of the file selector are: Browse files \ folders, file types, etc., all through the Java file class.
Knowledge Point Two, call method description
Uses the Startactivityforresult () initiator and the Onactivityresult () method to receive the callback information.
First paste the following:
There is nothing else to say, everyone look at the code comment, 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 adapters
- private void Setgridviewadapter (String filePath) {
- Updatefileitems (FilePath);
- Madatper = New Filechooseradapter (this, mfilelists);
- Mgridview.setadapter (Madatper);
- }
- //Update data according to Path, and notify Adatper data change
- 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 files
- 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 First Enter, the object of Madatper don ' t initialized
- if (madatper! = null)
- Madatper.notifydatasetchanged (); //re-refresh
- }
- //Get all files for 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 on the item as folder, display all files under that 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 level directory
- public void Backprocess () {
- //Determine if the current path is not a sdcard path, and if not, return to the previous layer.
- if (!mlastfilepath.equals (Msdcardrootpath)) {
- File Thisfile = new File (Mlastfilepath);
- String Parentfilepath = Thisfile.getparent ();
- Updatefileitems (Parentfilepath);
- }
- else { //is sdcard path, end directly
- Setresult (result_canceled);
- Finish ();
- }
- }
- }
The interface of this instance is slightly humble, but we can improve it on this basis and add other functions. This example code: http://download.csdn.net/detail/qinjuning/4825392.
Android File Selector instance sharing