Android programming-getting started development Folder Manager development file event operations-2

Source: Internet
Author: User

Android programming-getting started development Folder Manager development file event operations-2

In the previous blog, we have obtained the folder list. We need to add events to the file list subitem. For example, if we click a file, we will execute

Open Operation: Click the folder to open the folder, traverse the file list, and so on until the last is the file location.

Processing with folders will be discussed later


In my program, I wrote a class to process files. FileOpreationUitl:

Package com. example. util; import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import android. content. context; import android. util. log; import android. widget. toast;/*** file operation class * @ author Engineer-Jsp * @ date 2014.10.27 */public class FileOpreationUitl {public static Map
 
  
Mp3_List = new HashMap
  
   
(); Public static List
   
    
> Mp3_data = new ArrayList
    
     
> (); Public static String mp3_listitem; // public static Map
     
      
Picture_List = new HashMap
      
        (); Public static List
       
         > Picture_data = new ArrayList
        
          > (); Public static String picture_listitem; // public static Map
         
           Video_List = new HashMap
          
            (); Public static List
           
             > Video_data = new ArrayList
            
              > (); Public static String video_listitem; // delete the File and directory public void deleteFile (File path) {// If the transmitted parameter path is a File, directly perform the delete operation if (path. isFile () {// Delete path. delete (); // otherwise, it is a folder. Execute the following Operation} else {// defines the File list of the Path folder in which the File array receives the parameter File [] files = path. listFiles (); // looping the calendar File for (File f: files) {// if the File is executed if (f. isFile () {// Delete f. delete () ;}else {// call your own recursive deleteFile (f) ;}/// delete the directory path. delete () ;}// copy the public void copyFile (File currentpath, File srcpath) {File NewFile = new File (srcpath, currentpath. getName (); if (! NewFile. exists () {try {newFile. createNewFile (); FileInputStream fileinput = new FileInputStream (currentpath); FileOutputStream fileout = new FileOutputStream (newFile); byte [] byt = new byte [1024*16]; int length = 0; while (length = fileinput. read (byt ))! =-1) {fileout. write (byt, 0, length);} fileinput. close (); fileout. close ();} catch (IOException e) {e. printStackTrace () ;}// else {// newFile. delete (); // copyFile (currentpath, srcpath); // copy the public void copyDirectory (File currentpath, File srcpath) {if (currentpath. isFile () {copyFile (currentpath, srcpath);} else {File file = new File (srcpath, currentpath. getName (); if (! File. exists () {file. mkdir ();} // else {// file. delete (); //} File [] files = currentpath. listFiles (); for (File f: files) {copyDirectory (f, file) ;}// Delete the directory // currentpath. delete () ;}// create public void newFile (File currentpath) {if (! Currentpath. exists () {currentpath. mkdirs ();}} // Music category/***** @ param groupPath if you want to obtain the mp3 file under the SDcard, enter the sdcard path * using a recursive method */public void getReciver (File mp3_Path) {// obtain the Directory and file for (int I = 0; I <mp3_Path.listFiles () under the sdcard directory cyclically (). length; I ++) {File childFile = mp3_Path.listFiles () [I]; // if it is a directory, continue to call getSDcardFile () if (childFile. isDirectory () {getReciver (childFile);} else {// if it is a file, then the delimiter does not end with delimiter, but is added to the List if (childFile. toString (). endsWith (". mp3 ") {mp3_List.put (mp3_listitem, childFile. getName (). toString (); mp3_data.add (mp3_List); // print the file name System. out. println (childFile. getName (); Log. d ("XXXXXXXXXX", childFile. getName (); // print the file path System. out. println (childFile. getAbsolutePath (); Log. d ("XXXXXXXXXX", childFile. getAbsolutePath () ;}}/// image category public void getPicture (File picture_Path) {// cyclically retrieve the directories and files under the sdcard directory for (int I = 0; I
             
              
Multi-choice execution with MultiChoiceModeListener is better than setChoiceMode, so that the application can perform batch operations, including copying and deleting. The following shows the effect:

Run the new test and click the plus sign (+) in the upper right corner:



Click OK to generate a folder and refresh the list:



Next let's take a look at the batch copy operation. Long press the ListView Item and the small + sign disappears in the upper right corner. Generate the delete and copy buttons. Click the Item to select and change the background color of the selected Item:




Execute batch paste. Here I only clicked 5 items, so I only pasted 5 folders. You can see the icon in the upper right corner and restore the icon when no copy operation is available, in fact, after you click the copy button, a paste button will be added, and the paste will disappear:



Batch delete operation:



This is probably the case for file operations. If you need it, you can expand it on your own. I am here to help you learn it. Thank you ~



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.