I am developing an app which has a splash screen that downloads a couple of files, before the files start to download I want to check whether the files already exist or not, and if they exist I want to delete them. The code shown below contains the right file paths and the function to check if the file exists seems to work as the read out in Logcat states "file deleted".
However when I check on the phone itself I see that whenever I launch the app 2 more files are added to the folder with the same file name but with increasing numbers
e.g. Launch 1... I get
clientraw.txt
clientrawextra.txt
Launch 2... I get
clientraw.txt
clientrawextra.txt
clientraw-1.txt
clientrawextra-1.txt
and so on.....
Therefore it would seem the delete function isn't working, any help would be appreciated!
//Code that checks if the clientraw.txt file already exists, if so the file is deleted
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard.getAbsolutePath() +
"/Download", client);
Log.d("file path", String.valueOf(file));
if (file.exists()) {
file.delete();
Log.d("file", "file deleted");
}
File sdCardextra = Environment.getExternalStorageDirectory();
File fileextra = new File(sdCardextra.getAbsolutePath() +
"/Download", clientextra);
if (fileextra.exists()) {
fileextra.delete();
Log.d("file", "file deleted");
}
ready();
It seems that it doesn't delete the file fast enough? When I get rid of the ready();
method (The method that downloads the files) it does delete the files fine, so I'm thinking that the files start downloading before the previous files are deleted really stuck on this one?!
public static boolean deleteDirectory(File path) {
// TODO Auto-generated method stub
if( path.exists() ) {
File[] files = path.listFiles();
for(int i=0; i<files.length; i++) {
if(files[i].isDirectory()) {
deleteDirectory(files[i]);
}
else {
files[i].delete();
}
}
}
return(path.delete());
}
This Code will Help you.. And In Android Manifest You have to get Permission to make modification..
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With