简体   繁体   中英

How to hide existing folder in Android?

I have a existing folder (Old Folder name : xyz) in Sdcard, Whenever I try to rename this folder (New Folder name : .xyz) using toRename(). It return false and create a new folder (name : .xyz). Old Folder (name : xyz) is also visible in sdcard.

How to rename the existing folder to make a that Folder hidden in Android?

String dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/xyz";

File file = new File(dir);
StringdirHide = Environment.getExternalStorageDirectory().getAbsolutePath() + "/.xyz";
File fileHide = new File(dirHide);
if (!file.exists() && !fileHide.exists())
{
    fileHide.mkdir();
}
else if(file.exists())
{
    file.toRename(fileHide);
}

The method to rename is renameTo . The following code should work. Tell me if you face any problems.

String dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/xyz";

File file = new File(dir);
String dirHide = Environment.getExternalStorageDirectory().getAbsolutePath() + "/.xyz";
File fileHide = new File(dirHide);
if (file.exists() && !fileHide.exists()) {
    file.renameTo(fileHide);
} else if(!file.exists()) {
    fileHide.mkdir();
}

@Akashsingla19 I think problem is the folder u want to rename is not exist run following code Twice hope you will get your answer

if (!file.exists())
        {
            file.mkdir();
        }
        else if(file.exists())
        {
            file.renameTo(fileHide);
        }

In your code you are using some toRename() method, which i couldn't found anywhere in File class in android. Actual method of File class in android to rename folders and files is renameTo() . Check this method and try to use it and revert please.

Thanks.

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