简体   繁体   English

如何从Android中的列表视图重命名SD卡的文件夹

[英]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. 我正在尝试从SD卡获取特定文件夹名称的所有列表到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. 使用列表视图时,我已经给出了复选框,因此用户可以一次检查任何单个列表值,然后可以根据需要在SD卡中编辑或创建文件夹来执行操作。

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. 这里的Code是静态类,而pm是整数变量。 So when user click button based on pm variable value it will perform task weather to rename folder or create folder. 因此,当用户单击基于pm变量值的按钮 ,它将执行任务天气以重命名文件夹或创建文件夹。

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. 另请检查LogCat以查看是否显示了一些警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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