0

Please take a look at my code bellow. Basically, I was able to erase the information contained in my file, but the empty file still exists on my android device. Instead of completely erasing my file, it only removes the information contained in the file.

 public void removeCache(Uri uri){
    File delete= new File(uri.getPath());
    if(delete.exists()){
        if(delete.delete()){
            Log.d("Deleted", ""+uri);
        }
        boolean exists= delete.exists();
        Log.d("Deleted", "does it exist? " + exists);
    }
}

Log Message: D/Deleted: /storage/emulated/0/secretVideos/NHLPROMO.mp4 D/Deleted: does it exist? false

UPDATE: So basically, the file does not exist in its direct directory, but download folder in the Android default file viewer stills shows deleted files. This is really annoying and should be removed by google. As of now I do not see any solution to this problem.

Naman Jain
  • 321
  • 5
  • 21
  • `.delete()` returns a boolean value whether deletion was successful. Can you check this value? It should delete the file as well. How did you check if it is still there? – PKlumpp Mar 22 '18 at 15:13
  • Hey @PKlumpp. I go into my downloads folder (where my downloaded file is stored), and it still exists there but when I open the file it shows an error. So basically I have dead weight files in my download folder. Before, I would click the file and it would open up. So basically, it clears the data, but does not actually remove the file from my storage. As for the boolean, I just checked it returns true. Otherwise my log statement would not print. – Naman Jain Mar 22 '18 at 15:15
  • So you use an Explorer App for that? Maybe the explorer thinks the fill is still there, but when it tries to access it, it throws an error because it is not there anymore. Try to refresh/restart your explorer. If that does not help, check if the file still exists with delete.exists() after you deleted – PKlumpp Mar 22 '18 at 15:18
  • Ok. let me try that out. – Naman Jain Mar 22 '18 at 15:19
  • I just tried it out, my android prints false for exists. – Naman Jain Mar 22 '18 at 15:28
  • 1
    Then it should really be deleted and it is a problem of your explorer – PKlumpp Mar 22 '18 at 15:37
  • your right. It was the explorer. I updates when I restart it. However, when I have used apps in the past that delete stuff from my storage, the files would not be dormant in the file manager. Any solution for this? – Naman Jain Mar 22 '18 at 15:38
  • No. Actually, I just tested on my samsung device, and the file explorer still shows the files even after a restart. – Naman Jain Mar 22 '18 at 15:39

1 Answers1

0
File file = new File(audio.getPath());
            MediaScannerConnection.scanFile(context,
                    new String[]{file.toString()},
                    null, null);

You can try to update the path to see if it still exists or if there is any data

cottontail
  • 10,268
  • 18
  • 50
  • 51