简体   繁体   中英

How to rename folder of SD-Card from list-view in android

I am trying get all the list of particular folder name from SD-Card to list-view in android. I am giving two option to user they can create folder as well as they can rename folder from application it self.

With the list-view i have given check-box so user can check any single list value at a time and then can perform operation as they want of editing or creating folder in SD-Card.

Everything is working fine till creating folder, but my problem start's here, when user tries to rename folder name, then first time user can successfully changes the folder name but while doing same task second time, user is not able to rename same folder again.

What mistake i am doing, following is my code please help, Thanks in advance.

 private static List<String> myList = new ArrayList<String>();


public static void folder_create() {
    if (Code.pm == 1) {
        String updateFolder = Environment.getExternalStorageDirectory()
                .getPath() + "/AudioRecorder/";
        File filecall = null;
        filecall = new File(updateFolder);
        File list[] = filecall.listFiles();
        for (int i = 0; i < list.length; i++) {
            myList.add(list[i].getName());
        }
        int len = mListView.getCount();
        SparseBooleanArray checked = mListView.getCheckedItemPositions();
        for (int i = 0; i < len; i++)
            if (checked.get(i)) {
                String mUpdateName = myList.get(i);
                File file = new File(updateFolder + "/" + mUpdateName);
                System.out.println("=======File======" + file);
                File file2 = new File(updateFolder + "/" + Code.className);
                System.out.println("=======File2======" + file2);
                file.renameTo(file2);
            }
    } else {
        String filepath = Environment.getExternalStorageDirectory()
                .getPath();
        File file = new File(filepath, "/AudioRecorder/" + Code.className);
        if (!file.exists()) {
            file.mkdirs();
        }
    }
}

Here Code is static class and pm is integer variable. So when user click button based on pm variable value it will perform task weather to rename folder or create folder.

If I understand your issue, you're trying to rename a directory after having made 'some tasks'. Are those tasks made into this folder? If yes, be sure that you have closed your streams (creating / modifying content) like so:

stream.close();

Please also check the LogCat to see if some warnings are shown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM