Android 編程之入門開發檔案夾管理器開發檔案事件操作-2

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   os   ar   java   for   

上一篇部落格,我們已經得到了資料夾清單,我們需要對檔案清單子項添加事件,比如我們點擊的是檔案,就執行

開啟操作,點擊的是檔案夾執行開啟檔案夾操作,遍曆檔案清單,以此類推直到最後一個是檔案位置,關於檔案

與檔案夾的處理後面會講到


在我的程式裡,我寫了一個類,對檔案進行處理,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;/** * 檔案的操作類 * @author Engineer-Jsp * @date 2014.10.27 */public class FileOpreationUitl {public static Map<String,Object> mp3_List=new HashMap<String,Object>();public static List<Map<String, Object>>mp3_data=new ArrayList<Map<String,Object>>();public static String mp3_listitem;//public static Map<String,Object> picture_List=new HashMap<String,Object>();public static List<Map<String, Object>>picture_data=new ArrayList<Map<String,Object>>();public static String picture_listitem;//public static Map<String,Object> video_List=new HashMap<String,Object>();public static List<Map<String, Object>> video_data=new ArrayList<Map<String,Object>>();public static String video_listitem;//刪除檔案和目錄public void  deleteFile(File path){//如果傳來的參數path是檔案,直接執行刪除操作if(path.isFile()){//刪除path.delete();//否則為檔案夾,執行下面的操作}else{//定義檔案數組接收參數Path檔案夾的檔案清單File[] files=path.listFiles();//迴圈編曆檔案for(File  f : files){//如果是檔案執行if(f.isFile()){//刪除f.delete();}else{//調用自己遞迴deleteFile(f);}}//刪除目錄path.delete();}}//複製檔案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);//}}//複製檔案夾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);}//刪除目錄//currentpath.delete();}}//建立public void  newFile(File currentpath){if(!currentpath.exists()){currentpath.mkdirs();}}//音樂分類/** *  * @param groupPath  如果你想擷取SDcard下面的所以mp3檔案你就填sdcard路徑 * 用的是遞迴的方式擷取 */public void getReciver(File mp3_Path){//迴圈擷取sdcard目錄下面的目錄和檔案for(int i=0; i< mp3_Path.listFiles().length; i++){File childFile = mp3_Path.listFiles()[i];//假如是目錄的話就繼續調用getSDcardFile()將childFile作為參數傳遞的方法裡面if(childFile.isDirectory()){getReciver(childFile);}else{//如果是檔案的話,判斷是不是以.mp3結尾,是就加入到List裡面if(childFile.toString().endsWith(".mp3")){mp3_List.put(mp3_listitem,childFile.getName().toString());mp3_data.add(mp3_List);//列印檔案的檔案名稱System.out.println(childFile.getName());Log.d("XXXXXXXXXX",childFile.getName());//列印檔案的路徑System.out.println(childFile.getAbsolutePath());Log.d("XXXXXXXXXX",childFile.getAbsolutePath());}}}}    //圖片分類public void getPicture(File picture_Path){//迴圈擷取sdcard目錄下面的目錄和檔案for(int i=0; i<picture_Path.listFiles().length; i++){File childFile =picture_Path.listFiles()[i];//假如是目錄的話就繼續調用getSDcardFile()將childFile作為參數傳遞的方法裡面if(childFile.isDirectory()){getPicture(childFile);}else{//如果是檔案的話,判斷是不是以.mp3結尾,是就加入到List裡面if(childFile.toString().endsWith(".png")||childFile.toString().endsWith(".gif")||childFile.toString().endsWith(".bmp")||childFile.toString().endsWith(".jpg")){picture_List.put(picture_listitem,childFile.getName().toString());picture_data.add(picture_List);//列印檔案的檔案名稱System.out.println(childFile.getName());Log.d("XXXXXXXXXX",childFile.getName());//列印檔案的路徑System.out.println(childFile.getAbsolutePath());Log.d("XXXXXXXXXX",childFile.getAbsolutePath());}}}}//視頻分類public void getVideo(File video_Path){//迴圈擷取sdcard目錄下面的目錄和檔案for(int i=0; i<video_Path.listFiles().length; i++){File childFile = video_Path.listFiles()[i];//假如是目錄的話就繼續調用getSDcardFile()將childFile作為參數傳遞的方法裡面if(childFile.isDirectory()){getVideo(childFile);}else{//如果是檔案的話,判斷是不是以.mp3結尾,是就加入到List裡面if(childFile.toString().endsWith(".mp4")||childFile.toString().endsWith(".avi")||childFile.toString().endsWith(".rmvb")||childFile.toString().endsWith(".3gp")){video_List.put(video_listitem,childFile.getName().toString());video_data.add(video_List);//列印檔案的檔案名稱System.out.println(childFile.getName());Log.d("XXXXXXXXXX",childFile.getName());//列印檔案的路徑System.out.println(childFile.getAbsolutePath());Log.d("XXXXXXXXXX",childFile.getAbsolutePath());}}}}//搜尋//public void  searchFile(File path){////}}

配合 MultiChoiceModeListener 執行多選,優於 setChoiceMode 單選,讓application可以執行批量的操作處理,包括複製、刪除等,下面看看效果:

執行建立測試,點擊右上方小+號:



點擊確定,組建檔案夾,重新整理列表:



下面看看批量複製操作,長按ListView Item,右上方小+號消失,產生刪除按鈕和複製按鈕,點擊Item選中,更改選中Item項背景顏色:




執行批量粘貼,這裡我只點了5項,所以只粘貼了5個檔案夾,大家注意看右上方表徵圖,又恢複到了沒有複製操作的時候的表徵圖,其實在點擊複製按鈕之後,會添加一個粘貼按鈕,粘貼完後消失:



大量刪除操作:



檔案操作大概就寫了這些,有需要的可以自己拓展,我這裡主要是方便大夥學習,謝謝~



Android 編程之入門開發檔案夾管理器開發檔案事件操作-2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.