Android File Selector instance sharing

Source: Internet
Author: User

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
  1. Public class Copyoffilechooseractivity extends Activity {
  2. private String Msdcardrootpath; //sdcard Root Path
  3. private String Mlastfilepath; //The path currently displayed
  4. private arraylist<fileinfo> mfilelists;
  5. private Filechooseradapter Madatper;
  6. //Configure adapters
  7. private void Setgridviewadapter (String filePath) {
  8. Updatefileitems (FilePath);
  9. Madatper = New Filechooseradapter (this, mfilelists);
  10. Mgridview.setadapter (Madatper);
  11. }
  12. //Update data according to Path, and notify Adatper data change
  13. private void Updatefileitems (String filePath) {
  14. Mlastfilepath = FilePath;
  15. Mtvpath.settext (Mlastfilepath);
  16. if (mfilelists = = null)
  17. mfilelists = new arraylist<fileinfo> ();
  18. if (!mfilelists.isempty ())
  19. Mfilelists.clear ();
  20. file[] files = Folderscan (FilePath);
  21. if (files = = null)
  22. return;
  23. For (int i = 0; i < files.length; i++) {
  24. if (Files[i].ishidden ()) //Do not show hidden files
  25. continue;
  26. String Fileabsolutepath = Files[i].getabsolutepath ();
  27. String fileName = Files[i].getname ();
  28. Boolean isdirectory = false;
  29. if (files[i].isdirectory ()) {
  30. Isdirectory = true;
  31. }
  32. FileInfo FileInfo = new FileInfo (Fileabsolutepath, FileName, isdirectory);
  33. //Add to list
  34. Mfilelists.add (FileInfo);
  35. }
  36. //when First Enter, the object of Madatper don ' t initialized
  37. if (madatper! = null)
  38. Madatper.notifydatasetchanged (); //re-refresh
  39. }
  40. //Get all files for the current path
  41. Private file[] Folderscan (String path) {
  42. File File = new file (path);
  43. file[] files = file.listfiles ();
  44. return files;
  45. }
  46. private Adapterview.onitemclicklistener Mitemclicklistener = new Onitemclicklistener () {
  47. public void Onitemclick (adapterview<?> adapterview, view view, int position,
  48. Long ID) {  
  49. FileInfo FileInfo = (FileInfo) ((Filechooseradapter) Adapterview.getadapter ()). GetItem (position));
  50. if (fileinfo.isdirectory ()) //Click on the item as folder, display all files under that folder
  51. Updatefileitems (Fileinfo.getfilepath ());
  52. Else if (Fileinfo.ispptfile ()) { //is a PPT file, the path is notified to the caller
  53. Intent Intent = new Intent ();
  54. Intent.putextra (Extra_file_chooser, Fileinfo.getfilepath ());
  55. Setresult (RESULT_OK, intent);
  56. Finish ();
  57. }
  58. else { //other files .....
  59. Toast (GetText (R.string.open_file_error_format));
  60. }
  61. }
  62. };
  63. Public boolean onKeyDown (int keycode, keyevent event) {
  64. if (event.getaction () = = Keyevent.action_down && event.getkeycode ()
  65. = = Keyevent.keycode_back) {
  66. Backprocess ();
  67. return true;
  68. }
  69. return Super.onkeydown (KeyCode, event);
  70. }
  71. //Returns the operation of the previous level directory
  72. public void Backprocess () {
  73. //Determine if the current path is not a sdcard path, and if not, return to the previous layer.
  74. if (!mlastfilepath.equals (Msdcardrootpath)) {
  75. File Thisfile = new File (Mlastfilepath);
  76. String Parentfilepath = Thisfile.getparent ();
  77. Updatefileitems (Parentfilepath);
  78. }
  79. else { //is sdcard path, end directly
  80. Setresult (result_canceled);
  81. Finish ();
  82. }
  83. }
  84. }

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

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.