如何使用Environment.getExternalStorageDirectory移动文件?

时间:2017-11-04 00:11:39

标签: android file

我的代码重命名所有.mp3文件(将'audio only'替换为'')。我必须下载很多mp3文件,但无论如何,这很好用。

问题是,如何将这些文件移动到单独的文件夹中?我需要对现有代码进行哪些修改?

/ getting download folder path for filename fixer
File myDirectory = new File(Environment.getExternalStorageDirectory() + "/Download/");
public static final String[] TARGET_EXTENSIONS = {"mp3"};//target extensions 

class FilenameFixer extends AsyncTask<Void, Boolean, Boolean> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        renameInDir(myDirectory);
        return true;
    }

    private void renameInDir(File dir) {
        File listFile[] = dir.listFiles();
        if (listFile != null) {
            for (int i = 0; i < listFile.length; i++) {
                if (listFile[i].isDirectory()) {
                    renameInDir(listFile[i]);
                } else {
                    String fPath = listFile[i].getPath();
                    for (String ext : TARGET_EXTENSIONS) {
                         //(?i) means ignore the case
                        fPath = fPath.replace("audio only", " "); // empty
                    }
                    listFile[i].renameTo(new File(fPath));
                }
            }
        }
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
    }
}

0 个答案:

没有答案