其他分享
首页 > 其他分享> > android – 删除功能不起作用

android – 删除功能不起作用

作者:互联网

我正在开发一个应用程序,它有一个下载几个文件的启动画面,在文件开始下载之前我想检查文件是否已经存在,如果它们存在,我想删除它们.下面显示的代码包含正确的文件路径和检查文件是否存在的函数似乎在Logcat状态下读取“文件已删除”.

然而,当我检查手机本身时,我看到每当我启动应用程序时,会有更多文件添加到具有相同文件名但数量不断增加的文件夹中

例如发射1 …我明白了

clientraw.txt
clientrawextra.txt

发射2 …我明白了

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

等等…..

因此,似乎删除功能不起作用,任何帮助将不胜感激!

//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();

它似乎没有足够快地删除文件?当我摆脱ready();方法(下载文件的方法)它确实删除了文件,所以我认为文件开始下载之前删除的文件真的卡在这个文件上了?!

解决方法:

试试这个

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();
}

标签:file,android,delete-file,download
来源: https://codeday.me/bug/20190629/1325363.html