删除功能不起作用 [英] Delete function not working

查看:133
本文介绍了删除功能不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,它有一个启动画面,可以下载几个文件,然后才能开始下载文件,我想检查文件是否已经存在,如果存在,我想删除它们。下面显示的代码包含正确的文件路径和检查文件是否存在似乎在Logcat状态文件已删除中读取的功能。



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

例如启动1 ...我得到

  clientraw.txt 
clientrawextra.txt

启动2 ...我得到

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

等等.....



所以看来删除功能不行,任何帮助都不会赞赏! / p>

  //检查clientraw.txt文件是否已经存在的代码,如果是,则删除该文件
文件sdCard = Environment.getExternalStorageDirectory();
文件文件=新文件(sdCard.getAbsolutePath()+
/下载,客户端);
Log.d(file path,String.valueOf(file));
if(file.exists()){
file.delete();
Log.d(file,file deleted);
}

文件sdCardextra = Environment.getExternalStorageDirectory();
文件fileextra = new File(sdCardextra.getAbsolutePath()+
/ Download,clientextra);
if(fileextra.exists()){
fileextra.delete();
Log.d(file,file deleted);
}

ready();

看起来它没有足够的文件删除文件?当我摆脱 ready(); 方法(下载文件的方法),它会删除文件,所以我认为文件开始下载之前以前的文件被删除真的卡在这一个?!

解决方案

尝试这个

  void deleteExternalStoragePublicPicture(){
//创建一个路径,我们将把我们的图片放在用户的
// public下载目录中,并删除该文件。如果外部
//存储当前未安装,则将失败。
文件路径= Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS);
文件文件=新文件(路径,DemoPicture.jpg);
file.delete();
}


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?!

解决方案

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

这篇关于删除功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆