我可以获得文件夹的路径,但不能获得子文件夹

时间:2016-05-12 05:17:28

标签: android

任何人都可以解释为什么这不起作用?

我得到了这样的文件夹路径 -

// getting SDcard root path
File myDirectory = new File(Environment.getExternalStorageDirectory()
        + "/SecuDrive/");

这就是我用“x”

替换所有mp3(音频)文件名中的点的方法
@Override
    protected Boolean doInBackground(Void... params) {
        File listFile[] = myDirectory.listFiles();
        if (listFile != null) {
            for (int i = 0; i < listFile.length; i++) {
                if (listFile[i].isDirectory()) {

                } else {
                    String fPath = listFile[i].getPath();

                    for (String ext : TARGET_EXTENSIONS) {
                        fPath = fPath.replace("." + ext,
                                "x" + ext);
                    }

                    listFile[i].renameTo(new File(fPath));
                }
            }
        }
        return true;
    }
例如

-

来自ninja.mp3 - &gt; ninjaxmp3

但这不是重命名子目录中的文件。它只重命名父目录(/ SecuDrive /)

中的文件

2 个答案:

答案 0 :(得分:2)

  

试试这个

public void listFile(String pathname) {
File f = new File(pathname);
File[] listfiles = f.listFiles();
for (int i = 0; i < listfiles.length; i++) {
    if (listfiles[i].isDirectory()) {
        File[] internalFile = listfiles[i].listFiles();
        for (int j = 0; j < internalFile.length; j++) {
            System.out.println(internalFile[j]);
            if (internalFile[j].isDirectory()) {
                String name = internalFile[j].getAbsolutePath();
                listFile(name);
            }

        }
    } else {
        System.out.println(listfiles[i]);
    }

}

}

答案 1 :(得分:0)

  1. 您的代码效率不高。检查我的代码
  2. 2.Reason是你找不到Sub目录的文件。在您的代码中,当您找到目录时,需要递归搜索该目录。

        private void getFile(File dir) {
    
        File[] listFile = dir.listFiles(new FileFilter() {
            @Override
            public boolean accept(File dir) {
                String name = dir.toString();
    
                if (dir.isDirectory()) {
                    getFile(dir); //calling same method inside
                    return false;
                }
                else{
                    // you get all files here.. try to rename all files here. use variable "name" to do your action like renaming as you said
                }
                return false;
            }
        });
    
    }
    

    我建议您在项目中复制此代码并在doInBackground方法中调用getFile()。

    更新的答案:

        private class loaderexample extends AsyncTask<Void,Void,Void>{
    File myDirectory;
        private Context ctx;
        private loader(Context ctx){
            this.ctx=ctx;
        }
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
    myDirectory = new File(Environment.getExternalStorageDirectory()
        + "/SecuDrive/");
    
        }
    
        @Override
        protected Void doInBackground(Void... params) {
    
            getFile(myDirectory);
            return null;
        }
    
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
        }
    
        private void getFile(File dir) {
    
        File[] listFile = dir.listFiles(new FileFilter() {
            @Override
            public boolean accept(File dir) {
                String name = dir.toString();
    
                if (dir.isDirectory()) {
                    getFile(dir); //calling same method inside
                    return false;
                }
                else{
                    // you get all files here.. try to rename all files here. use variable "name" to do your action like renaming as you said
                }
                return false;
            }
        });
    
    }
    
    
    
    
    }
    
相关问题