问题
My app polls a directory on the SDcard for the appearance of new files that where dropped there by the user from Windows file explorer over the USB connection. When a new file appears, my app processes it, and then deletes it, however the file still shows up in Windows file explorer. I know the file is gone because it no longer appears in the DDMS file explorer, and my poller doesn't get triggered again. Any insights into how Android interacts with Windows file explorer would be appreciated. I've tried playing around with MediaScannerConnectionClient, which helps with getting programatically created directories to appear, but does nothing to get files to disappear.
I'm running Android 3.2 on an Acer Iconia A500. My PC is running Windows XP. The files are .csv and .txt files. I'm using File.delete() to delete them.
Thanks.
回答1:
This is an old issue, but the above answers were not really helpful to me, so I tried some other stuff and the following worked for me.
Just call the scanFile
method of the MediaScannerConnection
on the file to be deleted after you delete it:
File file = new File("...");
String absolutePathToFile = file.getAbsolutePath();
file.delete();
MediaScannerConnection.scanFile(context, new String[]{absolutePathToFile}, null, null);
I am guessing that the scanner scans the file location, does not find the file and updates the OS's file index or whatever takes care of making the files visible to the explorer.
回答2:
Android, being a Linux-based OS, will delete a file only when the last file handle to it is closed. The file name may disappear earlier, though.
On Windows, having an open file handle means the file name still exists. So Windows simply doesn't expect the file to disappear like it does.
回答3:
If the files are gone when you refresh, it may just be because Windows doesn't expect the directory to change out from underneath it, and so doesn't check for changes.
来源:https://stackoverflow.com/questions/8097768/programatically-deleted-files-still-show-up-in-windows-explorer