【发布时间】:2014-12-03 20:10:15
【问题描述】:
是否有一种简单的机制来确定 DownloadManager remove() 何时完成,因为它似乎是部分异步的。该函数几乎立即返回并计算下载表中已删除的条目数,但实际的文件系统管理似乎被推入了某个后台线程。
问题是我编写了一些代码,在提取新副本之前查找并删除文件 X 的任何现有 DownloadManager 条目(希望是文件系统对象)。不幸的是,新副本在文件系统管理开始之前就已进入目录,对于以前的化身。因此,内部管理实际上最终会在某个时候删除新版本,并在 DownloadManager 表中留下一个孤立条目。
可以通过某种方式阻止直到文件系统删除被操作。
调试代码:
DownloadManager.Query query = new DownloadManager.Query().setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL);
downloads = getAllDownloadIds(manager.query(query));
path = activity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
//If a file name was passed remove any existing entry / version of the file
if ( fileName != null && ! fileName.isEmpty() ){
if ( downloads.containsKey(fileName)){
ids = downloads.get(fileName);
for (Long id : ids) {
Uri path = manager.getUriForDownloadedFile(id);
File checkFile = new File(path.toString());
Log.e(TAG, "Removing existing file: " + path.toString() + ":" + id);
int entriesRemoved = manager.remove(id);
Log.e(TAG, "Existing files removed: " + entriesRemoved);
}
}
}
...
Log.v(TAG, "Attempting to create a file in the 'Download' directory on the external storage::" + path.toString() +"/"+ fileName);
file = new File(path, fileName);
Log.v(TAG, "Does the file already exist::" + file.exists());
示例输出:
… V/Export﹕ Removing existing file: file:///storage/sdcard/Download/appData.csv:101
… V/Export﹕ Existing files removed: 1
… V/Export﹕ Attempting to create a file in the 'Download' directory on the external storage::/storage/sdcard/Download/appData.csv
… V/Export﹕ Does the file already exist::true
【问题讨论】:
-
把你的目的地放在外部存储包文件夹中的一个临时文件夹中,然后自己删除。
标签: android android-download-manager download-manager