Android – deletion does not work

I am developing an application. It has a startup screen for downloading several files. Before downloading the files, I want to check whether the files already exist. If they exist, I want to delete them. The code shown below contains the correct file path and the function to check whether the files exist. It seems that the "file deleted" is read in the logcat state

However, when I check the phone itself, I see that every time I start the application, more files are added to an increasing number of folders with the same file name

For example, launch 1... I see

clientraw.txt
clientrawextra.txt

Launch 2... I see

clientraw.txt
clientrawextra.txt
clientraw-1.txt
clientrawextra-1.txt

Wait

Therefore, it seems that the deletion function does not work, and any help will 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 doesn't seem to delete files fast enough? When I get rid of ready (); Method (the method of downloading a file) it does delete the file, so I think the file deleted before the file download is really stuck in this file?!

resolvent:

Try this

void deleteExternalStoragePublicPicture() {
   // Create a path where we will place our picture in the user's
   // public download directory and delete the file.  If external
   // storage is not currently mounted this will fail.
      File path = Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_DOWNLOADS);
      File file = new File(path, "DemoPicture.jpg");
      file.delete();
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>