Android读取手机根目录文件即本地SD卡

版权声明:支持原创,注明出处。 https://blog.csdn.net/qq_38584967/article/details/79048702

前言

有需要所以学习了一下关于读取本地文件目录的知识,自己也仿照着写了个。哈哈,初学花了不少时间,感谢原文博客的分享!不废话见图。这里写图片描述
大家如果有学习需要可以去我GitHub下载,下载的兄弟给我右上角随手一个Star如何,感谢感谢。地址https://github.com/jianhaojiang/TestApp

下面是研究内容

效果图:



  1. package wuwang.tools.utils;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.IOException;  
  7. import java.text.DecimalFormat;  
  8. import java.util.ArrayList;  
  9. import java.util.Comparator;  
  10. import java.util.HashMap;  
  11. import java.util.List;  
  12. import java.util.Map;  
  13.   
  14. import android.os.Environment;  
  15.   
  16.   
  17. /** 
  18.  * 用于获取手机的文件夹及文件的工具类,如果权限允许,可以获取手机上任意路径的文件列表 
  19.  * GetFilesUtils使用的是懒汉式单例模式,线程安全 
  20.  *@author wuwang 
  21.  *@since 2014.11 
  22.  */  
  23. public class GetFilesUtils {  
  24.       
  25.     public static final String FILE_TYPE_FOLDER=“wFl2d”;  
  26.       
  27.     public static final String FILE_INFO_NAME=“fName”;  
  28.     public static final String FILE_INFO_ISFOLDER=“fIsDir”;  
  29.     public static final String FILE_INFO_TYPE=“fFileType”;  
  30.     public static final String FILE_INFO_NUM_SONDIRS=“fSonDirs”;  
  31.     public static final String FILE_INFO_NUM_SONFILES=“fSonFiles”;  
  32.     public static final String FILE_INFO_PATH=“fPath”;  
  33.       
  34.     private static GetFilesUtils gfu;  
  35.       
  36.     private GetFilesUtils(){  
  37.           
  38.     }  
  39.       
  40.     /** 
  41.      * 获取GetFilesUtils实例 
  42.      * @return GetFilesUtils 
  43.      **/  
  44.     public static synchronized GetFilesUtils getInstance(){  
  45.         if(gfu==null){  
  46.             gfu=new GetFilesUtils();  
  47.         }  
  48.         return gfu;  
  49.     }  
  50.       
  51.     /** 
  52.      * 获取文件path文件夹下的文件列表 
  53.      * @see #getSonNode(String) 
  54.      * @param path 手机上的文件夹 
  55.      * @return path文件夹下的文件列表的信息,信息存储在Map中,Map的key的列表如下:<br /> 
  56.      *         FILE_INFO_NAME : String 文件名称 <br /> 
  57.      *         FILE_INFO_ISFOLDER: boolean 是否为文件夹   <br /> 
  58.      *         FILE_INFO_TYPE: string 文件的后缀 <br /> 
  59.      *         FILE_INFO_NUM_SONDIRS : int 子文件夹个数   <br /> 
  60.      *         FILE_INFO_NUM_SONFILES: int 子文件个数    <br /> 
  61.      *         FILE_INFO_PATH : String 文件的绝对路径  <br /> 
  62.      **/  
  63.     public List<Map<String, Object>> getSonNode(File path){  
  64.         if(path.isDirectory()){  
  65.             List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();  
  66.             File[] files=path.listFiles();  
  67.             if(files!=null){  
  68.   
  69.                 for(int i=0;i<files.length;i++){  
  70.                     Map<String, Object> fileInfo=new HashMap<String, Object>();  
  71.                     fileInfo.put(FILE_INFO_NAME, files[i].getName());  
  72.                     if(files[i].isDirectory()){  
  73.                         fileInfo.put(FILE_INFO_ISFOLDER, true);  
  74.                         File[] bFiles=files[i].listFiles();  
  75.                         if(bFiles==null){  
  76.                             fileInfo.put(FILE_INFO_NUM_SONDIRS, 0);  
  77.                             fileInfo.put(FILE_INFO_NUM_SONFILES, 0);  
  78.                         }else{  
  79.                             int getNumOfDir=0;  
  80.                             for(int j=0;j<bFiles.length;j++){  
  81.                                 if(bFiles[j].isDirectory()){  
  82.                                     getNumOfDir++;  
  83.                                 }  
  84.                             }  
  85.                             fileInfo.put(FILE_INFO_NUM_SONDIRS, getNumOfDir);  
  86.                             fileInfo.put(FILE_INFO_NUM_SONFILES, bFiles.length-getNumOfDir);  
  87.                         }  
  88.                         fileInfo.put(FILE_INFO_TYPE, FILE_TYPE_FOLDER);  
  89.                     }else{  
  90.                         fileInfo.put(FILE_INFO_ISFOLDER, false);  
  91.                         fileInfo.put(FILE_INFO_NUM_SONDIRS, 0);  
  92.                         fileInfo.put(FILE_INFO_NUM_SONFILES, 0);  
  93.                         fileInfo.put(FILE_INFO_TYPE, getFileType(files[i].getName()));  
  94.                     }  
  95.                     fileInfo.put(FILE_INFO_PATH, files[i].getAbsoluteFile());  
  96.                     list.add(fileInfo);  
  97.                 }  
  98.                 return list;  
  99.             }else{  
  100.                 return null;  
  101.             }  
  102.         }else{  
  103.             return null;  
  104.         }  
  105.     }  
  106.     /** 
  107.      * 获取文件pathStr文件夹下的文件列表 
  108.      * @see #getSonNode(File) 
  109.      * @param pathStr 手机上的文件夹的绝对路径 
  110.      * @return pathStr文件夹下的文件列表的信息,信息存储在Map中,Map的key的列表如下:<br /> 
  111.      *         FILE_INFO_NAME : String 文件名称 <br /> 
  112.      *         FILE_INFO_ISFOLDER: boolean 是否为文件夹   <br /> 
  113.      *         FILE_INFO_TYPE: string 文件的后缀 <br /> 
  114.      *         FILE_INFO_NUM_SONDIRS : int 子文件夹个数   <br /> 
  115.      *         FILE_INFO_NUM_SONFILES: int 子文件个数    <br /> 
  116.      *         FILE_INFO_PATH : String 文件的绝对路径  <br /> 
  117.      **/  
  118.     public List<Map<String, Object>> getSonNode(String pathStr){  
  119.         File path=new File(pathStr);  
  120.         return getSonNode(path);  
  121.     }  
  122.       
  123.     /** 
  124.      * 获取文件path文件或文件夹的兄弟节点文件列表 
  125.      * @see #getBrotherNode(String) 
  126.      * @param path 手机上的文件夹 
  127.      * @return path文件夹下的文件列表的信息,信息存储在Map中,Map的key的列表如下:<br /> 
  128.      *         FILE_INFO_NAME : String 文件名称 <br /> 
  129.      *         FILE_INFO_ISFOLDER: boolean 是否为文件夹   <br /> 
  130.      *         FILE_INFO_TYPE: string 文件的后缀 <br /> 
  131.      *         FILE_INFO_NUM_SONDIRS : int 子文件夹个数   <br /> 
  132.      *         FILE_INFO_NUM_SONFILES: int 子文件个数    <br /> 
  133.      *         FILE_INFO_PATH : String 文件的绝对路径  <br /> 
  134.      **/  
  135.     public List<Map<String, Object>> getBrotherNode(File path){  
  136.         if(path.getParentFile()!=null){  
  137.             return getSonNode(path.getParentFile());  
  138.         }else{  
  139.             return null;  
  140.         }  
  141.     }  
  142.     /** 
  143.      * 获取文件path文件或文件夹的兄弟节点文件列表 
  144.      * @see #getBrotherNode(File) 
  145.      * @param path 手机上的文件夹 
  146.      * @return path文件夹下的文件列表的信息,信息存储在Map中,Map的key的列表如下:<br /> 
  147.      *         FILE_INFO_NAME : String 文件名称 <br /> 
  148.      *         FILE_INFO_ISFOLDER: boolean 是否为文件夹   <br /> 
  149.      *         FILE_INFO_TYPE: string 文件的后缀 <br /> 
  150.      *         FILE_INFO_NUM_SONDIRS : int 子文件夹个数   <br /> 
  151.      *         FILE_INFO_NUM_SONFILES: int 子文件个数    <br /> 
  152.      *         FILE_INFO_PATH : String 文件的绝对路径  <br /> 
  153.      **/  
  154.     public List<Map<String, Object>> getBrotherNode(String pathStr){  
  155.         File path=new File(pathStr);  
  156.         return getBrotherNode(path);  
  157.     }  
  158.       
  159.     /** 
  160.      * 获取文件或文件夹的父路径 
  161.      * @param File path文件或者文件夹 
  162.      * @return String path的父路径 
  163.      **/  
  164.     public String getParentPath(File path){  
  165.         if(path.getParentFile()==null){  
  166.             return null;  
  167.         }else{  
  168.             return path.getParent();  
  169.         }  
  170.     }  
  171.     /** 
  172.      * 获取文件或文件的父路径 
  173.      * @param String pathStr文件或者文件夹路径 
  174.      * @return String pathStr的父路径 
  175.      **/  
  176.     public String getParentPath(String pathStr){  
  177.         File path=new File(pathStr);  
  178.         if(path.getParentFile()==null){  
  179.             return null;  
  180.         }else{  
  181.             return path.getParent();  
  182.         }  
  183.     }  
  184.   
  185.     /** 
  186.      * 获取sd卡的绝对路径 
  187.      * @return String 如果sd卡存在,返回sd卡的绝对路径,否则返回null 
  188.      **/  
  189.     public String getSDPath(){  
  190.         String sdcard=Environment.getExternalStorageState();  
  191.         if(sdcard.equals(Environment.MEDIA_MOUNTED)){  
  192.             return Environment.getExternalStorageDirectory().getAbsolutePath();  
  193.         }else{  
  194.             return null;  
  195.         }  
  196.     }  
  197.       
  198.     /** 
  199.      * 获取一个基本的路径,一般应用创建存放应用数据可以用到 
  200.      * @return String 如果SD卡存在,返回SD卡的绝对路径,如果SD卡不存在,返回Android数据目录的绝对路径 
  201.      **/  
  202.     public String getBasePath(){  
  203.         String basePath=getSDPath();  
  204.         if(basePath==null){  
  205.             return Environment.getDataDirectory().getAbsolutePath();  
  206.         }else{  
  207.             return basePath;  
  208.         }  
  209.     }  
  210.       
  211.     /** 
  212.      * 获取文件path的大小 
  213.      * @return String path的大小 
  214.      **/  
  215.     public String getFileSize(File path) throws IOException{  
  216.         if(path.exists()){  
  217.             DecimalFormat df = new DecimalFormat(“#.00”);  
  218.             String sizeStr=”“;  
  219.             FileInputStream fis=new FileInputStream(path);  
  220.             long size=fis.available();  
  221.             fis.close();  
  222.             if(size<1024){  
  223.                 sizeStr=size+”B”;  
  224.             }else if(size<1048576){  
  225.                 sizeStr=df.format(size/(double)1024)+“KB”;  
  226.             }else if(size<1073741824){  
  227.                 sizeStr=df.format(size/(double)1048576)+“MB”;  
  228.             }else{  
  229.                 sizeStr=df.format(size/(double)1073741824)+“GB”;  
  230.             }  
  231.             return sizeStr;  
  232.         }else{  
  233.             return null;  
  234.         }  
  235.     }  
  236.       
  237.     /** 
  238.      * 获取文件fpath的大小 
  239.      * @return String path的大小 
  240.      **/  
  241.     public String getFileSize(String fpath){  
  242.         File path=new File(fpath);  
  243.         if(path.exists()){  
  244.             DecimalFormat df = new DecimalFormat(“#.00”);  
  245.             String sizeStr=”“;  
  246.             long size=0;  
  247.             try {  
  248.                 FileInputStream fis = new FileInputStream(path);  
  249.                 size=fis.available();  
  250.                 fis.close();  
  251.             } catch (FileNotFoundException e) {  
  252.                 // TODO Auto-generated catch block  
  253.                 e.printStackTrace();  
  254.                 return “未知大小”;  
  255.             } catch (IOException e) {  
  256.                 // TODO Auto-generated catch block  
  257.                 e.printStackTrace();  
  258.                 return “未知大小”;  
  259.             }  
  260.             if(size<1024){  
  261.                 sizeStr=size+”B”;  
  262.             }else if(size<1048576){  
  263.                 sizeStr=df.format(size/(double)1024)+“KB”;  
  264.             }else if(size<1073741824){  
  265.                 sizeStr=df.format(size/(double)1048576)+“MB”;  
  266.             }else{  
  267.                 sizeStr=df.format(size/(double)1073741824)+“GB”;  
  268.             }  
  269.             return sizeStr;  
  270.         }else{  
  271.             return “未知大小”;  
  272.         }  
  273.     }  
  274.       
  275.     /** 
  276.      * 根据后缀获取文件fileName的类型 
  277.      * @return String 文件的类型 
  278.      **/  
  279.     public String getFileType(String fileName){  
  280.         if(fileName!=“”&&fileName.length()>3){  
  281.             int dot=fileName.lastIndexOf(“.”);  
  282.             if(dot>0){  
  283.                 return fileName.substring(dot+1);  
  284.             }else{  
  285.                 return “”;  
  286.             }  
  287.         }  
  288.         return “”;  
  289.     }  
  290.       
  291.     public Comparator<Map<String, Object>> defaultOrder() {  
  292.           
  293.         final String orderBy0=FILE_INFO_ISFOLDER;  
  294.         final String orderBy1=FILE_INFO_TYPE;  
  295.         final String orderBy2=FILE_INFO_NAME;  
  296.           
  297.         Comparator<Map<String, Object>> order=new Comparator<Map<String,Object>>() {  
  298.   
  299.             @Override  
  300.             public int compare(Map<String, Object> lhs, Map<String, Object> rhs) {  
  301.                 // TODO Auto-generated method stub  
  302.                 int left0=lhs.get(orderBy0).equals(true)?0:1;  
  303.                 int right0=rhs.get(orderBy0).equals(true)?0:1;  
  304.                 if(left0==right0){  
  305.                     String left1=lhs.get(orderBy1).toString();  
  306.                     String right1=rhs.get(orderBy1).toString();  
  307.                     if(left1.compareTo(right1)==0){  
  308.                         String left2=lhs.get(orderBy2).toString();  
  309.                         String right2=rhs.get(orderBy2).toString();  
  310.                         return left2.compareTo(right2);  
  311.                     }else{  
  312.                         return left1.compareTo(right1);  
  313.                     }  
  314.                 }else{  
  315.                     return left0-right0;  
  316.                 }  
  317.             }  
  318.         };  
  319.   
  320.         return order;  
  321.     }  
  322.       
  323. }  
package wuwang.tools.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Environment;


/**
 * 用于获取手机的文件夹及文件的工具类,如果权限允许,可以获取手机上任意路径的文件列表
 * GetFilesUtils使用的是懒汉式单例模式,线程安全
 *@author wuwang
 *@since 2014.11
 */
public class GetFilesUtils {

    public static final String FILE_TYPE_FOLDER="wFl2d";

    public static final String FILE_INFO_NAME="fName";
    public static final String FILE_INFO_ISFOLDER="fIsDir";
    public static final String FILE_INFO_TYPE="fFileType";
    public static final String FILE_INFO_NUM_SONDIRS="fSonDirs";
    public static final String FILE_INFO_NUM_SONFILES="fSonFiles";
    public static final String FILE_INFO_PATH="fPath";

    private static GetFilesUtils gfu;

    private GetFilesUtils(){

    }

    /**
     * 获取GetFilesUtils实例
     * @return GetFilesUtils
     **/
    public static synchronized GetFilesUtils getInstance(){
        if(gfu==null){
            gfu=new GetFilesUtils();
        }
        return gfu;
    }

    /**
     * 获取文件path文件夹下的文件列表
     * @see #getSonNode(String)
     * @param path 手机上的文件夹
     * @return path文件夹下的文件列表的信息,信息存储在Map中,Map的key的列表如下:<br />
     *         FILE_INFO_NAME : String 文件名称 <br />
     *         FILE_INFO_ISFOLDER: boolean 是否为文件夹   <br />
     *         FILE_INFO_TYPE: string 文件的后缀 <br />
     *         FILE_INFO_NUM_SONDIRS : int 子文件夹个数   <br />
     *         FILE_INFO_NUM_SONFILES: int 子文件个数    <br />
     *         FILE_INFO_PATH : String 文件的绝对路径  <br />
     **/
    public List<Map<String, Object>> getSonNode(File path){
        if(path.isDirectory()){
            List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
            File[] files=path.listFiles();
            if(files!=null){

                for(int i=0;i<files.length;i++){
                    Map<String, Object> fileInfo=new HashMap<String, Object>();
                    fileInfo.put(FILE_INFO_NAME, files[i].getName());
                    if(files[i].isDirectory()){
                        fileInfo.put(FILE_INFO_ISFOLDER, true);
                        File[] bFiles=files[i].listFiles();
                        if(bFiles==null){
                            fileInfo.put(FILE_INFO_NUM_SONDIRS, 0);
                            fileInfo.put(FILE_INFO_NUM_SONFILES, 0);
                        }else{
                            int getNumOfDir=0;
                            for(int j=0;j<bFiles.length;j++){
                                if(bFiles[j].isDirectory()){
                                    getNumOfDir++;
                                }
                            }
                            fileInfo.put(FILE_INFO_NUM_SONDIRS, getNumOfDir);
                            fileInfo.put(FILE_INFO_NUM_SONFILES, bFiles.length-getNumOfDir);
                        }
                        fileInfo.put(FILE_INFO_TYPE, FILE_TYPE_FOLDER);
                    }else{
                        fileInfo.put(FILE_INFO_ISFOLDER, false);
                        fileInfo.put(FILE_INFO_NUM_SONDIRS, 0);
                        fileInfo.put(FILE_INFO_NUM_SONFILES, 0);
                        fileInfo.put(FILE_INFO_TYPE, getFileType(files[i].getName()));
                    }
                    fileInfo.put(FILE_INFO_PATH, files[i].getAbsoluteFile());
                    list.add(fileInfo);
                }
                return list;
            }else{
                return null;
            }
        }else{
            return null;
        }
    }
    /**
     * 获取文件pathStr文件夹下的文件列表
     * @see #getSonNode(File)
     * @param pathStr 手机上的文件夹的绝对路径
     * @return pathStr文件夹下的文件列表的信息,信息存储在Map中,Map的key的列表如下:<br />
     *         FILE_INFO_NAME : String 文件名称 <br />
     *         FILE_INFO_ISFOLDER: boolean 是否为文件夹   <br />
     *         FILE_INFO_TYPE: string 文件的后缀 <br />
     *         FILE_INFO_NUM_SONDIRS : int 子文件夹个数   <br />
     *         FILE_INFO_NUM_SONFILES: int 子文件个数    <br />
     *         FILE_INFO_PATH : String 文件的绝对路径  <br />
     **/
    public List<Map<String, Object>> getSonNode(String pathStr){
        File path=new File(pathStr);
        return getSonNode(path);
    }

    /**
     * 获取文件path文件或文件夹的兄弟节点文件列表
     * @see #getBrotherNode(String)
     * @param path 手机上的文件夹
     * @return path文件夹下的文件列表的信息,信息存储在Map中,Map的key的列表如下:<br />
     *         FILE_INFO_NAME : String 文件名称 <br />
     *         FILE_INFO_ISFOLDER: boolean 是否为文件夹   <br />
     *         FILE_INFO_TYPE: string 文件的后缀 <br />
     *         FILE_INFO_NUM_SONDIRS : int 子文件夹个数   <br />
     *         FILE_INFO_NUM_SONFILES: int 子文件个数    <br />
     *         FILE_INFO_PATH : String 文件的绝对路径  <br />
     **/
    public List<Map<String, Object>> getBrotherNode(File path){
        if(path.getParentFile()!=null){
            return getSonNode(path.getParentFile());
        }else{
            return null;
        }
    }
    /**
     * 获取文件path文件或文件夹的兄弟节点文件列表
     * @see #getBrotherNode(File)
     * @param path 手机上的文件夹
     * @return path文件夹下的文件列表的信息,信息存储在Map中,Map的key的列表如下:<br />
     *         FILE_INFO_NAME : String 文件名称 <br />
     *         FILE_INFO_ISFOLDER: boolean 是否为文件夹   <br />
     *         FILE_INFO_TYPE: string 文件的后缀 <br />
     *         FILE_INFO_NUM_SONDIRS : int 子文件夹个数   <br />
     *         FILE_INFO_NUM_SONFILES: int 子文件个数    <br />
     *         FILE_INFO_PATH : String 文件的绝对路径  <br />
     **/
    public List<Map<String, Object>> getBrotherNode(String pathStr){
        File path=new File(pathStr);
        return getBrotherNode(path);
    }

    /**
     * 获取文件或文件夹的父路径
     * @param File path文件或者文件夹
     * @return String path的父路径
     **/
    public String getParentPath(File path){
        if(path.getParentFile()==null){
            return null;
        }else{
            return path.getParent();
        }
    }
    /**
     * 获取文件或文件的父路径
     * @param String pathStr文件或者文件夹路径
     * @return String pathStr的父路径
     **/
    public String getParentPath(String pathStr){
        File path=new File(pathStr);
        if(path.getParentFile()==null){
            return null;
        }else{
            return path.getParent();
        }
    }

    /**
     * 获取sd卡的绝对路径
     * @return String 如果sd卡存在,返回sd卡的绝对路径,否则返回null
     **/
    public String getSDPath(){
        String sdcard=Environment.getExternalStorageState();
        if(sdcard.equals(Environment.MEDIA_MOUNTED)){
            return Environment.getExternalStorageDirectory().getAbsolutePath();
        }else{
            return null;
        }
    }

    /**
     * 获取一个基本的路径,一般应用创建存放应用数据可以用到
     * @return String 如果SD卡存在,返回SD卡的绝对路径,如果SD卡不存在,返回Android数据目录的绝对路径
     **/
    public String getBasePath(){
        String basePath=getSDPath();
        if(basePath==null){
            return Environment.getDataDirectory().getAbsolutePath();
        }else{
            return basePath;
        }
    }

    /**
     * 获取文件path的大小
     * @return String path的大小
     **/
    public String getFileSize(File path) throws IOException{
        if(path.exists()){
            DecimalFormat df = new DecimalFormat("#.00");
            String sizeStr="";
            FileInputStream fis=new FileInputStream(path);
            long size=fis.available();
            fis.close();
            if(size<1024){
                sizeStr=size+"B";
            }else if(size<1048576){
                sizeStr=df.format(size/(double)1024)+"KB";
            }else if(size<1073741824){
                sizeStr=df.format(size/(double)1048576)+"MB";
            }else{
                sizeStr=df.format(size/(double)1073741824)+"GB";
            }
            return sizeStr;
        }else{
            return null;
        }
    }

    /**
     * 获取文件fpath的大小
     * @return String path的大小
     **/
    public String getFileSize(String fpath){
        File path=new File(fpath);
        if(path.exists()){
            DecimalFormat df = new DecimalFormat("#.00");
            String sizeStr="";
            long size=0;
            try {
                FileInputStream fis = new FileInputStream(path);
                size=fis.available();
                fis.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return "未知大小";
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return "未知大小";
            }
            if(size<1024){
                sizeStr=size+"B";
            }else if(size<1048576){
                sizeStr=df.format(size/(double)1024)+"KB";
            }else if(size<1073741824){
                sizeStr=df.format(size/(double)1048576)+"MB";
            }else{
                sizeStr=df.format(size/(double)1073741824)+"GB";
            }
            return sizeStr;
        }else{
            return "未知大小";
        }
    }

    /**
     * 根据后缀获取文件fileName的类型
     * @return String 文件的类型
     **/
    public String getFileType(String fileName){
        if(fileName!=""&&fileName.length()>3){
            int dot=fileName.lastIndexOf(".");
            if(dot>0){
                return fileName.substring(dot+1);
            }else{
                return "";
            }
        }
        return "";
    }

    public Comparator<Map<String, Object>> defaultOrder() {

        final String orderBy0=FILE_INFO_ISFOLDER;
        final String orderBy1=FILE_INFO_TYPE;
        final String orderBy2=FILE_INFO_NAME;

        Comparator<Map<String, Object>> order=new Comparator<Map<String,Object>>() {

            @Override
            public int compare(Map<String, Object> lhs, Map<String, Object> rhs) {
                // TODO Auto-generated method stub
                int left0=lhs.get(orderBy0).equals(true)?0:1;
                int right0=rhs.get(orderBy0).equals(true)?0:1;
                if(left0==right0){
                    String left1=lhs.get(orderBy1).toString();
                    String right1=rhs.get(orderBy1).toString();
                    if(left1.compareTo(right1)==0){
                        String left2=lhs.get(orderBy2).toString();
                        String right2=rhs.get(orderBy2).toString();
                        return left2.compareTo(right2);
                    }else{
                        return left1.compareTo(right1);
                    }
                }else{
                    return left0-right0;
                }
            }
        };

        return order;
    }

}


使用方法:List<Map<String, Object>> list=GetFilesUtils.getInstance().getSonNode(file); //或其他方法

使用示例:

  1. package wuwang.mypage.activity;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  5. import java.util.Collections;  
  6. import java.util.HashMap;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9.   
  10. import wuwang.ebookworm.R;  
  11. import wuwang.tools.utils.GetFilesUtils;  
  12. import android.app.Activity;  
  13. import android.os.Bundle;  
  14. import android.view.View;  
  15. import android.view.View.OnClickListener;  
  16. import android.widget.AdapterView;  
  17. import android.widget.AdapterView.OnItemClickListener;  
  18. import android.widget.ListView;  
  19. import android.widget.SimpleAdapter;  
  20. import android.widget.TextView;  
  21. import android.widget.Toast;  
  22.   
  23. public class FolderActivity extends Activity implements OnItemClickListener,OnClickListener {  
  24.   
  25.     private ListView folderLv;  
  26.     private TextView foldernowTv;  
  27.     private SimpleAdapter sAdapter;  
  28.     private List<Map<String, Object>> aList;  
  29.     private String baseFile;  
  30.       
  31.     private TextView titleTv;  
  32.       
  33.     @Override  
  34.     protected void onCreate(Bundle savedInstanceState) {  
  35.         // TODO Auto-generated method stub  
  36.         super.onCreate(savedInstanceState);  
  37.         setContentView(R.layout.mypage_folder);  
  38.         baseFile=GetFilesUtils.getInstance().getBasePath();  
  39.           
  40.         titleTv=(TextView) findViewById(R.id.mtitle);  
  41.         titleTv.setText(”本地文件”);  
  42.           
  43.         folderLv=(ListView) findViewById(R.id.folder_list);  
  44.         foldernowTv=(TextView) findViewById(R.id.folder_now);  
  45.         foldernowTv.setText(baseFile);  
  46.         foldernowTv.setOnClickListener(this);  
  47.         aList=new ArrayList<Map<String,Object>>();  
  48.         sAdapter=new SimpleAdapter(this, aList,R.layout.listitem_folder, new String[]{“fImg”,“fName”,“fInfo”},  
  49.                 new int[]{R.id.folder_img,R.id.folder_name,R.id.folder_info});  
  50.         folderLv.setAdapter(sAdapter);  
  51.         folderLv.setOnItemClickListener(this);  
  52.         try {  
  53.             loadFolderList(baseFile);  
  54.         } catch (IOException e) {  
  55.             // TODO Auto-generated catch block  
  56.             e.printStackTrace();  
  57.         }  
  58.     }  
  59.       
  60.     private void loadFolderList(String file) throws IOException{  
  61.         List<Map<String, Object>> list=GetFilesUtils.getInstance().getSonNode(file);  
  62.         if(list!=null){  
  63.             Collections.sort(list, GetFilesUtils.getInstance().defaultOrder());  
  64.             aList.clear();  
  65.             for(Map<String, Object> map:list){  
  66.                 String fileType=(String) map.get(GetFilesUtils.FILE_INFO_TYPE);  
  67.                 Map<String,Object> gMap=new HashMap<String, Object>();  
  68.                 if(map.get(GetFilesUtils.FILE_INFO_ISFOLDER).equals(true)){  
  69.                     gMap.put(”fIsDir”true);  
  70.                     gMap.put(”fImg”,R.drawable.filetype_folder );  
  71.                     gMap.put(”fInfo”, map.get(GetFilesUtils.FILE_INFO_NUM_SONDIRS)+“个文件夹和”+  
  72.                             map.get(GetFilesUtils.FILE_INFO_NUM_SONFILES)+”个文件”);  
  73.                 }else{  
  74.                     gMap.put(”fIsDir”false);  
  75.                     if(fileType.equals(“txt”)||fileType.equals(“text”)){  
  76.                         gMap.put(”fImg”, R.drawable.filetype_text);  
  77.                     }else{  
  78.                         gMap.put(”fImg”, R.drawable.filetype_unknow);  
  79.                     }  
  80.                     gMap.put(”fInfo”,“文件大小:”+GetFilesUtils.getInstance().getFileSize(map.get(GetFilesUtils.FILE_INFO_PATH).toString()));  
  81.                 }  
  82.                 gMap.put(”fName”, map.get(GetFilesUtils.FILE_INFO_NAME));  
  83.                 gMap.put(”fPath”, map.get(GetFilesUtils.FILE_INFO_PATH));  
  84.                 aList.add(gMap);  
  85.             }  
  86.         }else{  
  87.             aList.clear();  
  88.         }  
  89.         sAdapter.notifyDataSetChanged();  
  90.         foldernowTv.setText(file);  
  91.     }  
  92.   
  93.     @Override  
  94.     public void onItemClick(AdapterView<?> parent, View view, int position,  
  95.             long id) {  
  96.         // TODO Auto-generated method stub  
  97.         try {  
  98.             if(aList.get(position).get(“fIsDir”).equals(true)){  
  99.                 loadFolderList(aList.get(position).get(”fPath”).toString());  
  100.             }else{  
  101.                 Toast.makeText(this“这是文件,处理程序待添加”, Toast.LENGTH_SHORT).show();  
  102.             }  
  103.         } catch (IOException e) {  
  104.             // TODO Auto-generated catch block  
  105.             e.printStackTrace();  
  106.         }  
  107.     }  
  108.   
  109.     @Override  
  110.     public void onClick(View v) {  
  111.         // TODO Auto-generated method stub  
  112.         if(v.getId()==R.id.folder_now){  
  113.             try {  
  114.                 String folder=GetFilesUtils.getInstance().getParentPath(foldernowTv.getText().toString());  
  115.                 if(folder==null){  
  116.                     Toast.makeText(this“无父目录,待处理”, Toast.LENGTH_SHORT).show();  
  117.                 }else{  
  118.                     loadFolderList(folder);  
  119.                 }  
  120.             } catch (IOException e) {  
  121.                 // TODO Auto-generated catch block  
  122.                 e.printStackTrace();  
  123.             }  
  124.         }  
  125.     }  
  126.       
  127. }  
package wuwang.mypage.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import wuwang.ebookworm.R;
import wuwang.tools.utils.GetFilesUtils;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class FolderActivity extends Activity implements OnItemClickListener,OnClickListener {

    private ListView folderLv;
    private TextView foldernowTv;
    private SimpleAdapter sAdapter;
    private List<Map<String, Object>> aList;
    private String baseFile;

    private TextView titleTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mypage_folder);
        baseFile=GetFilesUtils.getInstance().getBasePath();

        titleTv=(TextView) findViewById(R.id.mtitle);
        titleTv.setText("本地文件");

        folderLv=(ListView) findViewById(R.id.folder_list);
        foldernowTv=(TextView) findViewById(R.id.folder_now);
        foldernowTv.setText(baseFile);
        foldernowTv.setOnClickListener(this);
        aList=new ArrayList<Map<String,Object>>();
        sAdapter=new SimpleAdapter(this, aList,R.layout.listitem_folder, new String[]{"fImg","fName","fInfo"},
                new int[]{R.id.folder_img,R.id.folder_name,R.id.folder_info});
        folderLv.setAdapter(sAdapter);
        folderLv.setOnItemClickListener(this);
        try {
            loadFolderList(baseFile);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void loadFolderList(String file) throws IOException{
        List<Map<String, Object>> list=GetFilesUtils.getInstance().getSonNode(file);
        if(list!=null){
            Collections.sort(list, GetFilesUtils.getInstance().defaultOrder());
            aList.clear();
            for(Map<String, Object> map:list){
                String fileType=(String) map.get(GetFilesUtils.FILE_INFO_TYPE);
                Map<String,Object> gMap=new HashMap<String, Object>();
                if(map.get(GetFilesUtils.FILE_INFO_ISFOLDER).equals(true)){
                    gMap.put("fIsDir", true);
                    gMap.put("fImg",R.drawable.filetype_folder );
                    gMap.put("fInfo", map.get(GetFilesUtils.FILE_INFO_NUM_SONDIRS)+"个文件夹和"+
                            map.get(GetFilesUtils.FILE_INFO_NUM_SONFILES)+"个文件");
                }else{
                    gMap.put("fIsDir", false);
                    if(fileType.equals("txt")||fileType.equals("text")){
                        gMap.put("fImg", R.drawable.filetype_text);
                    }else{
                        gMap.put("fImg", R.drawable.filetype_unknow);
                    }
                    gMap.put("fInfo","文件大小:"+GetFilesUtils.getInstance().getFileSize(map.get(GetFilesUtils.FILE_INFO_PATH).toString()));
                }
                gMap.put("fName", map.get(GetFilesUtils.FILE_INFO_NAME));
                gMap.put("fPath", map.get(GetFilesUtils.FILE_INFO_PATH));
                aList.add(gMap);
            }
        }else{
            aList.clear();
        }
        sAdapter.notifyDataSetChanged();
        foldernowTv.setText(file);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub
        try {
            if(aList.get(position).get("fIsDir").equals(true)){
                loadFolderList(aList.get(position).get("fPath").toString());
            }else{
                Toast.makeText(this, "这是文件,处理程序待添加", Toast.LENGTH_SHORT).show();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v.getId()==R.id.folder_now){
            try {
                String folder=GetFilesUtils.getInstance().getParentPath(foldernowTv.getText().toString());
                if(folder==null){
                    Toast.makeText(this, "无父目录,待处理", Toast.LENGTH_SHORT).show();
                }else{
                    loadFolderList(folder);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

参考来源http://blog.csdn.net/junzia/article/details/41649063#insertcode 页面的布局文件为:

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     android:layout_width=“match_parent”  
  4.     android:layout_height=“match_parent”  
  5.     android:orientation=“vertical”  
  6.     android:background=“@color/frame_main_bg” >  
  7.     <include layout=“@layout/mypage_title_folder”/>  
  8.     <TextView android:layout_width=“match_parent”  
  9.         android:layout_height=“40dp”  
  10.         android:textColor=“#FFFFFF”  
  11.         android:textSize=“16sp”  
  12.         android:gravity=“center_vertical”  
  13.         android:ellipsize=“start”  
  14.         android:singleLine=“true”  
  15.         android:drawableLeft=“@drawable/folder_backupimg”  
  16.         android:paddingLeft=“10dp”  
  17.         android:drawablePadding=“10dp”  
  18.         android:id=“@+id/folder_now”  
  19.         android:background=“@color/frame_title_bg_clk_color”  />  
  20.     <ListView android:layout_width=“match_parent”  
  21.         android:layout_height=“wrap_content”  
  22.         android:id=“@+id/folder_list”  
  23.         android:dividerHeight=“1dp”  
  24.         android:divider=“@color/folder_list_divider” >  
  25.     </ListView>  
  26. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/frame_main_bg" >
    <include layout="@layout/mypage_title_folder"/>
    <TextView android:layout_width="match_parent"
        android:layout_height="40dp"
        android:textColor="#FFFFFF"
        android:textSize="16sp"
        android:gravity="center_vertical"
        android:ellipsize="start"
        android:singleLine="true"
        android:drawableLeft="@drawable/folder_backupimg"
        android:paddingLeft="10dp"
        android:drawablePadding="10dp"
        android:id="@+id/folder_now"
        android:background="@color/frame_title_bg_clk_color"  />
    <ListView android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/folder_list"
        android:dividerHeight="1dp"
        android:divider="@color/folder_list_divider" >
    </ListView>
</LinearLayout>


效果图:



  1. package wuwang.tools.utils;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.IOException;  
  7. import java.text.DecimalFormat;  
  8. import java.util.ArrayList;  
  9. import java.util.Comparator;  
  10. import java.util.HashMap;  
  11. import java.util.List;  
  12. import java.util.Map;  
  13.   
  14. import android.os.Environment;  
  15.   
  16.   
  17. /** 
  18.  * 用于获取手机的文件夹及文件的工具类,如果权限允许,可以获取手机上任意路径的文件列表 
  19.  * GetFilesUtils使用的是懒汉式单例模式,线程安全 
  20.  *@author wuwang 
  21.  *@since 2014.11 
  22.  */  
  23. public class GetFilesUtils {  
  24.       
  25.     public static final String FILE_TYPE_FOLDER=“wFl2d”;  
  26.       
  27.     public static final String FILE_INFO_NAME=“fName”;  
  28.     public static final String FILE_INFO_ISFOLDER=“fIsDir”;  
  29.     public static final String FILE_INFO_TYPE=“fFileType”;  
  30.     public static final String FILE_INFO_NUM_SONDIRS=“fSonDirs”;  
  31.     public static final String FILE_INFO_NUM_SONFILES=“fSonFiles”;  
  32.     public static final String FILE_INFO_PATH=“fPath”;  
  33.       
  34.     private static GetFilesUtils gfu;  
  35.       
  36.     private GetFilesUtils(){  
  37.           
  38.     }  
  39.       
  40.     /** 
  41.      * 获取GetFilesUtils实例 
  42.      * @return GetFilesUtils 
  43.      **/  
  44.     public static synchronized GetFilesUtils getInstance(){  
  45.         if(gfu==null){  
  46.             gfu=new GetFilesUtils();  
  47.         }  
  48.         return gfu;  
  49.     }  
  50.       
  51.     /** 
  52.      * 获取文件path文件夹下的文件列表 
  53.      * @see #getSonNode(String) 
  54.      * @param path 手机上的文件夹 
  55.      * @return path文件夹下的文件列表的信息,信息存储在Map中,Map的key的列表如下:<br /> 
  56.      *         FILE_INFO_NAME : String 文件名称 <br /> 
  57.      *         FILE_INFO_ISFOLDER: boolean 是否为文件夹   <br /> 
  58.      *         FILE_INFO_TYPE: string 文件的后缀 <br /> 
  59.      *         FILE_INFO_NUM_SONDIRS : int 子文件夹个数   <br /> 
  60.      *         FILE_INFO_NUM_SONFILES: int 子文件个数    <br /> 
  61.      *         FILE_INFO_PATH : String 文件的绝对路径  <br /> 
  62.      **/  
  63.     public List<Map<String, Object>> getSonNode(File path){  
  64.         if(path.isDirectory()){  
  65.             List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();  
  66.             File[] files=path.listFiles();  
  67.             if(files!=null){  
  68.   
  69.                 for(int i=0;i<files.length;i++){  
  70.                     Map<String, Object> fileInfo=new HashMap<String, Object>();  
  71.                     fileInfo.put(FILE_INFO_NAME, files[i].getName());  
  72.                     if(files[i].isDirectory()){  
  73.                         fileInfo.put(FILE_INFO_ISFOLDER, true);  
  74.                         File[] bFiles=files[i].listFiles();  
  75.                         if(bFiles==null){  
  76.                             fileInfo.put(FILE_INFO_NUM_SONDIRS, 0);  
  77.                             fileInfo.put(FILE_INFO_NUM_SONFILES, 0);  
  78.                         }else{  
  79.                             int getNumOfDir=0;  
  80.                             for(int j=0;j<bFiles.length;j++){  
  81.                                 if(bFiles[j].isDirectory()){  
  82.                                     getNumOfDir++;  
  83.                                 }  
  84.                             }  
  85.                             fileInfo.put(FILE_INFO_NUM_SONDIRS, getNumOfDir);  
  86.                             fileInfo.put(FILE_INFO_NUM_SONFILES, bFiles.length-getNumOfDir);  
  87.                         }  
  88.                         fileInfo.put(FILE_INFO_TYPE, FILE_TYPE_FOLDER);  
  89.                     }else{  
  90.                         fileInfo.put(FILE_INFO_ISFOLDER, false);  
  91.                         fileInfo.put(FILE_INFO_NUM_SONDIRS, 0);  
  92.                         fileInfo.put(FILE_INFO_NUM_SONFILES, 0);  
  93.                         fileInfo.put(FILE_INFO_TYPE, getFileType(files[i].getName()));  
  94.                     }  
  95.                     fileInfo.put(FILE_INFO_PATH, files[i].getAbsoluteFile());  
  96.                     list.add(fileInfo);  
  97.                 }  
  98.                 return list;  
  99.             }else{  
  100.                 return null;  
  101.             }  
  102.         }else{  
  103.             return null;  
  104.         }  
  105.     }  
  106.     /** 
  107.      * 获取文件pathStr文件夹下的文件列表 
  108.      * @see #getSonNode(File) 
  109.      * @param pathStr 手机上的文件夹的绝对路径 
  110.      * @return pathStr文件夹下的文件列表的信息,信息存储在Map中,Map的key的列表如下:<br /> 
  111.      *         FILE_INFO_NAME : String 文件名称 <br /> 
  112.      *         FILE_INFO_ISFOLDER: boolean 是否为文件夹   <br /> 
  113.      *         FILE_INFO_TYPE: string 文件的后缀 <br /> 
  114.      *         FILE_INFO_NUM_SONDIRS : int 子文件夹个数   <br /> 
  115.      *         FILE_INFO_NUM_SONFILES: int 子文件个数    <br /> 
  116.      *         FILE_INFO_PATH : String 文件的绝对路径  <br /> 
  117.      **/  
  118.     public List<Map<String, Object>> getSonNode(String pathStr){  
  119.         File path=new File(pathStr);  
  120.         return getSonNode(path);  
  121.     }  
  122.       
  123.     /** 
  124.      * 获取文件path文件或文件夹的兄弟节点文件列表 
  125.      * @see #getBrotherNode(String) 
  126.      * @param path 手机上的文件夹 
  127.      * @return path文件夹下的文件列表的信息,信息存储在Map中,Map的key的列表如下:<br /> 
  128.      *         FILE_INFO_NAME : String 文件名称 <br /> 
  129.      *         FILE_INFO_ISFOLDER: boolean 是否为文件夹   <br /> 
  130.      *         FILE_INFO_TYPE: string 文件的后缀 <br /> 
  131.      *         FILE_INFO_NUM_SONDIRS : int 子文件夹个数   <br /> 
  132.      *         FILE_INFO_NUM_SONFILES: int 子文件个数    <br /> 
  133.      *         FILE_INFO_PATH : String 文件的绝对路径  <br /> 
  134.      **/  
  135.     public List<Map<String, Object>> getBrotherNode(File path){  
  136.         if(path.getParentFile()!=null){  
  137.             return getSonNode(path.getParentFile());  
  138.         }else{  
  139.             return null;  
  140.         }  
  141.     }  
  142.     /** 
  143.      * 获取文件path文件或文件夹的兄弟节点文件列表 
  144.      * @see #getBrotherNode(File) 
  145.      * @param path 手机上的文件夹 
  146.      * @return path文件夹下的文件列表的信息,信息存储在Map中,Map的key的列表如下:<br /> 
  147.      *         FILE_INFO_NAME : String 文件名称 <br /> 
  148.      *         FILE_INFO_ISFOLDER: boolean 是否为文件夹   <br /> 
  149.      *         FILE_INFO_TYPE: string 文件的后缀 <br /> 
  150.      *         FILE_INFO_NUM_SONDIRS : int 子文件夹个数   <br /> 
  151.      *         FILE_INFO_NUM_SONFILES: int 子文件个数    <br /> 
  152.      *         FILE_INFO_PATH : String 文件的绝对路径  <br /> 
  153.      **/  
  154.     public List<Map<String, Object>> getBrotherNode(String pathStr){  
  155.         File path=new File(pathStr);  
  156.         return getBrotherNode(path);  
  157.     }  
  158.       
  159.     /** 
  160.      * 获取文件或文件夹的父路径 
  161.      * @param File path文件或者文件夹 
  162.      * @return String path的父路径 
  163.      **/  
  164.     public String getParentPath(File path){  
  165.         if(path.getParentFile()==null){  
  166.             return null;  
  167.         }else{  
  168.             return path.getParent();  
  169.         }  
  170.     }  
  171.     /** 
  172.      * 获取文件或文件的父路径 
  173.      * @param String pathStr文件或者文件夹路径 
  174.      * @return String pathStr的父路径 
  175.      **/  
  176.     public String getParentPath(String pathStr){  
  177.         File path=new File(pathStr);  
  178.         if(path.getParentFile()==null){  
  179.             return null;  
  180.         }else{  
  181.             return path.getParent();  
  182.         }  
  183.     }  
  184.   
  185.     /** 
  186.      * 获取sd卡的绝对路径 
  187.      * @return String 如果sd卡存在,返回sd卡的绝对路径,否则返回null 
  188.      **/  
  189.     public String getSDPath(){  
  190.         String sdcard=Environment.getExternalStorageState();  
  191.         if(sdcard.equals(Environment.MEDIA_MOUNTED)){  
  192.             return Environment.getExternalStorageDirectory().getAbsolutePath();  
  193.         }else{  
  194.             return null;  
  195.         }  
  196.     }  
  197.       
  198.     /** 
  199.      * 获取一个基本的路径,一般应用创建存放应用数据可以用到 
  200.      * @return String 如果SD卡存在,返回SD卡的绝对路径,如果SD卡不存在,返回Android数据目录的绝对路径 
  201.      **/  
  202.     public String getBasePath(){  
  203.         String basePath=getSDPath();  
  204.         if(basePath==null){  
  205.             return Environment.getDataDirectory().getAbsolutePath();  
  206.         }else{  
  207.             return basePath;  
  208.         }  
  209.     }  
  210.       
  211.     /** 
  212.      * 获取文件path的大小 
  213.      * @return String path的大小 
  214.      **/  
  215.     public String getFileSize(File path) throws IOException{  
  216.         if(path.exists()){  
  217.             DecimalFormat df = new DecimalFormat(“#.00”);  
  218.             String sizeStr=”“;  
  219.             FileInputStream fis=new FileInputStream(path);  
  220.             long size=fis.available();  
  221.             fis.close();  
  222.             if(size<1024){  
  223.                 sizeStr=size+”B”;  
  224.             }else if(size<1048576){  
  225.                 sizeStr=df.format(size/(double)1024)+“KB”;  
  226.             }else if(size<1073741824){  
  227.                 sizeStr=df.format(size/(double)1048576)+“MB”;  
  228.             }else{  
  229.                 sizeStr=df.format(size/(double)1073741824)+“GB”;  
  230.             }  
  231.             return sizeStr;  
  232.         }else{  
  233.             return null;  
  234.         }  
  235.     }  
  236.       
  237.     /** 
  238.      * 获取文件fpath的大小 
  239.      * @return String path的大小 
  240.      **/  
  241.     public String getFileSize(String fpath){  
  242.         File path=new File(fpath);  
  243.         if(path.exists()){  
  244.             DecimalFormat df = new DecimalFormat(“#.00”);  
  245.             String sizeStr=”“;  
  246.             long size=0;  
  247.             try {  
  248.                 FileInputStream fis = new FileInputStream(path);  
  249.                 size=fis.available();  
  250.                 fis.close();  
  251.             } catch (FileNotFoundException e) {  
  252.                 // TODO Auto-generated catch block  
  253.                 e.printStackTrace();  
  254.                 return “未知大小”;  
  255.             } catch (IOException e) {  
  256.                 // TODO Auto-generated catch block  
  257.                 e.printStackTrace();  
  258.                 return “未知大小”;  
  259.             }  
  260.             if(size<1024){  
  261.                 sizeStr=size+”B”;  
  262.             }else if(size<1048576){  
  263.                 sizeStr=df.format(size/(double)1024)+“KB”;  
  264.             }else if(size<1073741824){  
  265.                 sizeStr=df.format(size/(double)1048576)+“MB”;  
  266.             }else{  
  267.                 sizeStr=df.format(size/(double)1073741824)+“GB”;  
  268.             }  
  269.             return sizeStr;  
  270.         }else{  
  271.             return “未知大小”;  
  272.         }  
  273.     }  
  274.       
  275.     /** 
  276.      * 根据后缀获取文件fileName的类型 
  277.      * @return String 文件的类型 
  278.      **/  
  279.     public String getFileType(String fileName){  
  280.         if(fileName!=“”&&fileName.length()>3){  
  281.             int dot=fileName.lastIndexOf(“.”);  
  282.             if(dot>0){  
  283.                 return fileName.substring(dot+1);  
  284.             }else{  
  285.                 return “”;  
  286.             }  
  287.         }  
  288.         return “”;  
  289.     }  
  290.       
  291.     public Comparator<Map<String, Object>> defaultOrder() {  
  292.           
  293.         final String orderBy0=FILE_INFO_ISFOLDER;  
  294.         final String orderBy1=FILE_INFO_TYPE;  
  295.         final String orderBy2=FILE_INFO_NAME;  
  296.           
  297.         Comparator<Map<String, Object>> order=new Comparator<Map<String,Object>>() {  
  298.   
  299.             @Override  
  300.             public int compare(Map<String, Object> lhs, Map<String, Object> rhs) {  
  301.                 // TODO Auto-generated method stub  
  302.                 int left0=lhs.get(orderBy0).equals(true)?0:1;  
  303.                 int right0=rhs.get(orderBy0).equals(true)?0:1;  
  304.                 if(left0==right0){  
  305.                     String left1=lhs.get(orderBy1).toString();  
  306.                     String right1=rhs.get(orderBy1).toString();  
  307.                     if(left1.compareTo(right1)==0){  
  308.                         String left2=lhs.get(orderBy2).toString();  
  309.                         String right2=rhs.get(orderBy2).toString();  
  310.                         return left2.compareTo(right2);  
  311.                     }else{  
  312.                         return left1.compareTo(right1);  
  313.                     }  
  314.                 }else{  
  315.                     return left0-right0;  
  316.                 }  
  317.             }  
  318.         };  
  319.   
  320.         return order;  
  321.     }  
  322.       
  323. }  
package wuwang.tools.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Environment;


/**
 * 用于获取手机的文件夹及文件的工具类,如果权限允许,可以获取手机上任意路径的文件列表
 * GetFilesUtils使用的是懒汉式单例模式,线程安全
 *@author wuwang
 *@since 2014.11
 */
public class GetFilesUtils {

    public static final String FILE_TYPE_FOLDER="wFl2d";

    public static final String FILE_INFO_NAME="fName";
    public static final String FILE_INFO_ISFOLDER="fIsDir";
    public static final String FILE_INFO_TYPE="fFileType";
    public static final String FILE_INFO_NUM_SONDIRS="fSonDirs";
    public static final String FILE_INFO_NUM_SONFILES="fSonFiles";
    public static final String FILE_INFO_PATH="fPath";

    private static GetFilesUtils gfu;

    private GetFilesUtils(){

    }

    /**
     * 获取GetFilesUtils实例
     * @return GetFilesUtils
     **/
    public static synchronized GetFilesUtils getInstance(){
        if(gfu==null){
            gfu=new GetFilesUtils();
        }
        return gfu;
    }

    /**
     * 获取文件path文件夹下的文件列表
     * @see #getSonNode(String)
     * @param path 手机上的文件夹
     * @return path文件夹下的文件列表的信息,信息存储在Map中,Map的key的列表如下:<br />
     *         FILE_INFO_NAME : String 文件名称 <br />
     *         FILE_INFO_ISFOLDER: boolean 是否为文件夹   <br />
     *         FILE_INFO_TYPE: string 文件的后缀 <br />
     *         FILE_INFO_NUM_SONDIRS : int 子文件夹个数   <br />
     *         FILE_INFO_NUM_SONFILES: int 子文件个数    <br />
     *         FILE_INFO_PATH : String 文件的绝对路径  <br />
     **/
    public List<Map<String, Object>> getSonNode(File path){
        if(path.isDirectory()){
            List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
            File[] files=path.listFiles();
            if(files!=null){

                for(int i=0;i<files.length;i++){
                    Map<String, Object> fileInfo=new HashMap<String, Object>();
                    fileInfo.put(FILE_INFO_NAME, files[i].getName());
                    if(files[i].isDirectory()){
                        fileInfo.put(FILE_INFO_ISFOLDER, true);
                        File[] bFiles=files[i].listFiles();
                        if(bFiles==null){
                            fileInfo.put(FILE_INFO_NUM_SONDIRS, 0);
                            fileInfo.put(FILE_INFO_NUM_SONFILES, 0);
                        }else{
                            int getNumOfDir=0;
                            for(int j=0;j<bFiles.length;j++){
                                if(bFiles[j].isDirectory()){
                                    getNumOfDir++;
                                }
                            }
                            fileInfo.put(FILE_INFO_NUM_SONDIRS, getNumOfDir);
                            fileInfo.put(FILE_INFO_NUM_SONFILES, bFiles.length-getNumOfDir);
                        }
                        fileInfo.put(FILE_INFO_TYPE, FILE_TYPE_FOLDER);
                    }else{
                        fileInfo.put(FILE_INFO_ISFOLDER, false);
                        fileInfo.put(FILE_INFO_NUM_SONDIRS, 0);
                        fileInfo.put(FILE_INFO_NUM_SONFILES, 0);
                        fileInfo.put(FILE_INFO_TYPE, getFileType(files[i].getName()));
                    }
                    fileInfo.put(FILE_INFO_PATH, files[i].getAbsoluteFile());
                    list.add(fileInfo);
                }
                return list;
            }else{
                return null;
            }
        }else{
            return null;
        }
    }
    /**
     * 获取文件pathStr文件夹下的文件列表
     * @see #getSonNode(File)
     * @param pathStr 手机上的文件夹的绝对路径
     * @return pathStr文件夹下的文件列表的信息,信息存储在Map中,Map的key的列表如下:<br />
     *         FILE_INFO_NAME : String 文件名称 <br />
     *         FILE_INFO_ISFOLDER: boolean 是否为文件夹   <br />
     *         FILE_INFO_TYPE: string 文件的后缀 <br />
     *         FILE_INFO_NUM_SONDIRS : int 子文件夹个数   <br />
     *         FILE_INFO_NUM_SONFILES: int 子文件个数    <br />
     *         FILE_INFO_PATH : String 文件的绝对路径  <br />
     **/
    public List<Map<String, Object>> getSonNode(String pathStr){
        File path=new File(pathStr);
        return getSonNode(path);
    }

    /**
     * 获取文件path文件或文件夹的兄弟节点文件列表
     * @see #getBrotherNode(String)
     * @param path 手机上的文件夹
     * @return path文件夹下的文件列表的信息,信息存储在Map中,Map的key的列表如下:<br />
     *         FILE_INFO_NAME : String 文件名称 <br />
     *         FILE_INFO_ISFOLDER: boolean 是否为文件夹   <br />
     *         FILE_INFO_TYPE: string 文件的后缀 <br />
     *         FILE_INFO_NUM_SONDIRS : int 子文件夹个数   <br />
     *         FILE_INFO_NUM_SONFILES: int 子文件个数    <br />
     *         FILE_INFO_PATH : String 文件的绝对路径  <br />
     **/
    public List<Map<String, Object>> getBrotherNode(File path){
        if(path.getParentFile()!=null){
            return getSonNode(path.getParentFile());
        }else{
            return null;
        }
    }
    /**
     * 获取文件path文件或文件夹的兄弟节点文件列表
     * @see #getBrotherNode(File)
     * @param path 手机上的文件夹
     * @return path文件夹下的文件列表的信息,信息存储在Map中,Map的key的列表如下:<br />
     *         FILE_INFO_NAME : String 文件名称 <br />
     *         FILE_INFO_ISFOLDER: boolean 是否为文件夹   <br />
     *         FILE_INFO_TYPE: string 文件的后缀 <br />
     *         FILE_INFO_NUM_SONDIRS : int 子文件夹个数   <br />
     *         FILE_INFO_NUM_SONFILES: int 子文件个数    <br />
     *         FILE_INFO_PATH : String 文件的绝对路径  <br />
     **/
    public List<Map<String, Object>> getBrotherNode(String pathStr){
        File path=new File(pathStr);
        return getBrotherNode(path);
    }

    /**
     * 获取文件或文件夹的父路径
     * @param File path文件或者文件夹
     * @return String path的父路径
     **/
    public String getParentPath(File path){
        if(path.getParentFile()==null){
            return null;
        }else{
            return path.getParent();
        }
    }
    /**
     * 获取文件或文件的父路径
     * @param String pathStr文件或者文件夹路径
     * @return String pathStr的父路径
     **/
    public String getParentPath(String pathStr){
        File path=new File(pathStr);
        if(path.getParentFile()==null){
            return null;
        }else{
            return path.getParent();
        }
    }

    /**
     * 获取sd卡的绝对路径
     * @return String 如果sd卡存在,返回sd卡的绝对路径,否则返回null
     **/
    public String getSDPath(){
        String sdcard=Environment.getExternalStorageState();
        if(sdcard.equals(Environment.MEDIA_MOUNTED)){
            return Environment.getExternalStorageDirectory().getAbsolutePath();
        }else{
            return null;
        }
    }

    /**
     * 获取一个基本的路径,一般应用创建存放应用数据可以用到
     * @return String 如果SD卡存在,返回SD卡的绝对路径,如果SD卡不存在,返回Android数据目录的绝对路径
     **/
    public String getBasePath(){
        String basePath=getSDPath();
        if(basePath==null){
            return Environment.getDataDirectory().getAbsolutePath();
        }else{
            return basePath;
        }
    }

    /**
     * 获取文件path的大小
     * @return String path的大小
     **/
    public String getFileSize(File path) throws IOException{
        if(path.exists()){
            DecimalFormat df = new DecimalFormat("#.00");
            String sizeStr="";
            FileInputStream fis=new FileInputStream(path);
            long size=fis.available();
            fis.close();
            if(size<1024){
                sizeStr=size+"B";
            }else if(size<1048576){
                sizeStr=df.format(size/(double)1024)+"KB";
            }else if(size<1073741824){
                sizeStr=df.format(size/(double)1048576)+"MB";
            }else{
                sizeStr=df.format(size/(double)1073741824)+"GB";
            }
            return sizeStr;
        }else{
            return null;
        }
    }

    /**
     * 获取文件fpath的大小
     * @return String path的大小
     **/
    public String getFileSize(String fpath){
        File path=new File(fpath);
        if(path.exists()){
            DecimalFormat df = new DecimalFormat("#.00");
            String sizeStr="";
            long size=0;
            try {
                FileInputStream fis = new FileInputStream(path);
                size=fis.available();
                fis.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return "未知大小";
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return "未知大小";
            }
            if(size<1024){
                sizeStr=size+"B";
            }else if(size<1048576){
                sizeStr=df.format(size/(double)1024)+"KB";
            }else if(size<1073741824){
                sizeStr=df.format(size/(double)1048576)+"MB";
            }else{
                sizeStr=df.format(size/(double)1073741824)+"GB";
            }
            return sizeStr;
        }else{
            return "未知大小";
        }
    }

    /**
     * 根据后缀获取文件fileName的类型
     * @return String 文件的类型
     **/
    public String getFileType(String fileName){
        if(fileName!=""&&fileName.length()>3){
            int dot=fileName.lastIndexOf(".");
            if(dot>0){
                return fileName.substring(dot+1);
            }else{
                return "";
            }
        }
        return "";
    }

    public Comparator<Map<String, Object>> defaultOrder() {

        final String orderBy0=FILE_INFO_ISFOLDER;
        final String orderBy1=FILE_INFO_TYPE;
        final String orderBy2=FILE_INFO_NAME;

        Comparator<Map<String, Object>> order=new Comparator<Map<String,Object>>() {

            @Override
            public int compare(Map<String, Object> lhs, Map<String, Object> rhs) {
                // TODO Auto-generated method stub
                int left0=lhs.get(orderBy0).equals(true)?0:1;
                int right0=rhs.get(orderBy0).equals(true)?0:1;
                if(left0==right0){
                    String left1=lhs.get(orderBy1).toString();
                    String right1=rhs.get(orderBy1).toString();
                    if(left1.compareTo(right1)==0){
                        String left2=lhs.get(orderBy2).toString();
                        String right2=rhs.get(orderBy2).toString();
                        return left2.compareTo(right2);
                    }else{
                        return left1.compareTo(right1);
                    }
                }else{
                    return left0-right0;
                }
            }
        };

        return order;
    }

}


使用方法:List<Map<String, Object>> list=GetFilesUtils.getInstance().getSonNode(file); //或其他方法

使用示例:

  1. package wuwang.mypage.activity;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  5. import java.util.Collections;  
  6. import java.util.HashMap;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9.   
  10. import wuwang.ebookworm.R;  
  11. import wuwang.tools.utils.GetFilesUtils;  
  12. import android.app.Activity;  
  13. import android.os.Bundle;  
  14. import android.view.View;  
  15. import android.view.View.OnClickListener;  
  16. import android.widget.AdapterView;  
  17. import android.widget.AdapterView.OnItemClickListener;  
  18. import android.widget.ListView;  
  19. import android.widget.SimpleAdapter;  
  20. import android.widget.TextView;  
  21. import android.widget.Toast;  
  22.   
  23. public class FolderActivity extends Activity implements OnItemClickListener,OnClickListener {  
  24.   
  25.     private ListView folderLv;  
  26.     private TextView foldernowTv;  
  27.     private SimpleAdapter sAdapter;  
  28.     private List<Map<String, Object>> aList;  
  29.     private String baseFile;  
  30.       
  31.     private TextView titleTv;  
  32.       
  33.     @Override  
  34.     protected void onCreate(Bundle savedInstanceState) {  
  35.         // TODO Auto-generated method stub  
  36.         super.onCreate(savedInstanceState);  
  37.         setContentView(R.layout.mypage_folder);  
  38.         baseFile=GetFilesUtils.getInstance().getBasePath();  
  39.           
  40.         titleTv=(TextView) findViewById(R.id.mtitle);  
  41.         titleTv.setText(”本地文件”);  
  42.           
  43.         folderLv=(ListView) findViewById(R.id.folder_list);  
  44.         foldernowTv=(TextView) findViewById(R.id.folder_now);  
  45.         foldernowTv.setText(baseFile);  
  46.         foldernowTv.setOnClickListener(this);  
  47.         aList=new ArrayList<Map<String,Object>>();  
  48.         sAdapter=new SimpleAdapter(this, aList,R.layout.listitem_folder, new String[]{“fImg”,“fName”,“fInfo”},  
  49.                 new int[]{R.id.folder_img,R.id.folder_name,R.id.folder_info});  
  50.         folderLv.setAdapter(sAdapter);  
  51.         folderLv.setOnItemClickListener(this);  
  52.         try {  
  53.             loadFolderList(baseFile);  
  54.         } catch (IOException e) {  
  55.             // TODO Auto-generated catch block  
  56.             e.printStackTrace();  
  57.         }  
  58.     }  
  59.       
  60.     private void loadFolderList(String file) throws IOException{  
  61.         List<Map<String, Object>> list=GetFilesUtils.getInstance().getSonNode(file);  
  62.         if(list!=null){  
  63.             Collections.sort(list, GetFilesUtils.getInstance().defaultOrder());  
  64.             aList.clear();  
  65.             for(Map<String, Object> map:list){  
  66.                 String fileType=(String) map.get(GetFilesUtils.FILE_INFO_TYPE);  
  67.                 Map<String,Object> gMap=new HashMap<String, Object>();  
  68.                 if(map.get(GetFilesUtils.FILE_INFO_ISFOLDER).equals(true)){  
  69.                     gMap.put(”fIsDir”true);  
  70.                     gMap.put(”fImg”,R.drawable.filetype_folder );  
  71.                     gMap.put(”fInfo”, map.get(GetFilesUtils.FILE_INFO_NUM_SONDIRS)+“个文件夹和”+  
  72.                             map.get(GetFilesUtils.FILE_INFO_NUM_SONFILES)+”个文件”);  
  73.                 }else{  
  74.                     gMap.put(”fIsDir”false);  
  75.                     if(fileType.equals(“txt”)||fileType.equals(“text”)){  
  76.                         gMap.put(”fImg”, R.drawable.filetype_text);  
  77.                     }else{  
  78.                         gMap.put(”fImg”, R.drawable.filetype_unknow);  
  79.                     }  
  80.                     gMap.put(”fInfo”,“文件大小:”+GetFilesUtils.getInstance().getFileSize(map.get(GetFilesUtils.FILE_INFO_PATH).toString()));  
  81.                 }  
  82.                 gMap.put(”fName”, map.get(GetFilesUtils.FILE_INFO_NAME));  
  83.                 gMap.put(”fPath”, map.get(GetFilesUtils.FILE_INFO_PATH));  
  84.                 aList.add(gMap);  
  85.             }  
  86.         }else{  
  87.             aList.clear();  
  88.         }  
  89.         sAdapter.notifyDataSetChanged();  
  90.         foldernowTv.setText(file);  
  91.     }  
  92.   
  93.     @Override  
  94.     public void onItemClick(AdapterView<?> parent, View view, int position,  
  95.             long id) {  
  96.         // TODO Auto-generated method stub  
  97.         try {  
  98.             if(aList.get(position).get(“fIsDir”).equals(true)){  
  99.                 loadFolderList(aList.get(position).get(”fPath”).toString());  
  100.             }else{  
  101.                 Toast.makeText(this“这是文件,处理程序待添加”, Toast.LENGTH_SHORT).show();  
  102.             }  
  103.         } catch (IOException e) {  
  104.             // TODO Auto-generated catch block  
  105.             e.printStackTrace();  
  106.         }  
  107.     }  
  108.   
  109.     @Override  
  110.     public void onClick(View v) {  
  111.         // TODO Auto-generated method stub  
  112.         if(v.getId()==R.id.folder_now){  
  113.             try {  
  114.                 String folder=GetFilesUtils.getInstance().getParentPath(foldernowTv.getText().toString());  
  115.                 if(folder==null){  
  116.                     Toast.makeText(this“无父目录,待处理”, Toast.LENGTH_SHORT).show();  
  117.                 }else{  
  118.                     loadFolderList(folder);  
  119.                 }  
  120.             } catch (IOException e) {  
  121.                 // TODO Auto-generated catch block  
  122.                 e.printStackTrace();  
  123.             }  
  124.         }  
  125.     }  
  126.       
  127. }  
package wuwang.mypage.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import wuwang.ebookworm.R;
import wuwang.tools.utils.GetFilesUtils;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class FolderActivity extends Activity implements OnItemClickListener,OnClickListener {

    private ListView folderLv;
    private TextView foldernowTv;
    private SimpleAdapter sAdapter;
    private List<Map<String, Object>> aList;
    private String baseFile;

    private TextView titleTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mypage_folder);
        baseFile=GetFilesUtils.getInstance().getBasePath();

        titleTv=(TextView) findViewById(R.id.mtitle);
        titleTv.setText("本地文件");

        folderLv=(ListView) findViewById(R.id.folder_list);
        foldernowTv=(TextView) findViewById(R.id.folder_now);
        foldernowTv.setText(baseFile);
        foldernowTv.setOnClickListener(this);
        aList=new ArrayList<Map<String,Object>>();
        sAdapter=new SimpleAdapter(this, aList,R.layout.listitem_folder, new String[]{"fImg","fName","fInfo"},
                new int[]{R.id.folder_img,R.id.folder_name,R.id.folder_info});
        folderLv.setAdapter(sAdapter);
        folderLv.setOnItemClickListener(this);
        try {
            loadFolderList(baseFile);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void loadFolderList(String file) throws IOException{
        List<Map<String, Object>> list=GetFilesUtils.getInstance().getSonNode(file);
        if(list!=null){
            Collections.sort(list, GetFilesUtils.getInstance().defaultOrder());
            aList.clear();
            for(Map<String, Object> map:list){
                String fileType=(String) map.get(GetFilesUtils.FILE_INFO_TYPE);
                Map<String,Object> gMap=new HashMap<String, Object>();
                if(map.get(GetFilesUtils.FILE_INFO_ISFOLDER).equals(true)){
                    gMap.put("fIsDir", true);
                    gMap.put("fImg",R.drawable.filetype_folder );
                    gMap.put("fInfo", map.get(GetFilesUtils.FILE_INFO_NUM_SONDIRS)+"个文件夹和"+
                            map.get(GetFilesUtils.FILE_INFO_NUM_SONFILES)+"个文件");
                }else{
                    gMap.put("fIsDir", false);
                    if(fileType.equals("txt")||fileType.equals("text")){
                        gMap.put("fImg", R.drawable.filetype_text);
                    }else{
                        gMap.put("fImg", R.drawable.filetype_unknow);
                    }
                    gMap.put("fInfo","文件大小:"+GetFilesUtils.getInstance().getFileSize(map.get(GetFilesUtils.FILE_INFO_PATH).toString()));
                }
                gMap.put("fName", map.get(GetFilesUtils.FILE_INFO_NAME));
                gMap.put("fPath", map.get(GetFilesUtils.FILE_INFO_PATH));
                aList.add(gMap);
            }
        }else{
            aList.clear();
        }
        sAdapter.notifyDataSetChanged();
        foldernowTv.setText(file);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub
        try {
            if(aList.get(position).get("fIsDir").equals(true)){
                loadFolderList(aList.get(position).get("fPath").toString());
            }else{
                Toast.makeText(this, "这是文件,处理程序待添加", Toast.LENGTH_SHORT).show();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v.getId()==R.id.folder_now){
            try {
                String folder=GetFilesUtils.getInstance().getParentPath(foldernowTv.getText().toString());
                if(folder==null){
                    Toast.makeText(this, "无父目录,待处理", Toast.LENGTH_SHORT).show();
                }else{
                    loadFolderList(folder);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

参考来源http://blog.csdn.net/junzia/article/details/41649063#insertcode 页面的布局文件为:

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     android:layout_width=“match_parent”  
  4.     android:layout_height=“match_parent”  
  5.     android:orientation=“vertical”  
  6.     android:background=“@color/frame_main_bg” >  
  7.     <include layout=“@layout/mypage_title_folder”/>  
  8.     <TextView android:layout_width=“match_parent”  
  9.         android:layout_height=“40dp”  
  10.         android:textColor=“#FFFFFF”  
  11.         android:textSize=“16sp”  
  12.         android:gravity=“center_vertical”  
  13.         android:ellipsize=“start”  
  14.         android:singleLine=“true”  
  15.         android:drawableLeft=“@drawable/folder_backupimg”  
  16.         android:paddingLeft=“10dp”  
  17.         android:drawablePadding=“10dp”  
  18.         android:id=“@+id/folder_now”  
  19.         android:background=“@color/frame_title_bg_clk_color”  />  
  20.     <ListView android:layout_width=“match_parent”  
  21.         android:layout_height=“wrap_content”  
  22.         android:id=“@+id/folder_list”  
  23.         android:dividerHeight=“1dp”  
  24.         android:divider=“@color/folder_list_divider” >  
  25.     </ListView>  
  26. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/frame_main_bg" >
    <include layout="@layout/mypage_title_folder"/>
    <TextView android:layout_width="match_parent"
        android:layout_height="40dp"
        android:textColor="#FFFFFF"
        android:textSize="16sp"
        android:gravity="center_vertical"
        android:ellipsize="start"
        android:singleLine="true"
        android:drawableLeft="@drawable/folder_backupimg"
        android:paddingLeft="10dp"
        android:drawablePadding="10dp"
        android:id="@+id/folder_now"
        android:background="@color/frame_title_bg_clk_color"  />
    <ListView android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/folder_list"
        android:dividerHeight="1dp"
        android:divider="@color/folder_list_divider" >
    </ListView>
</LinearLayout>


猜你喜欢

转载自blog.csdn.net/qq_38584967/article/details/79048702