使用DownloadManager时我遇到了一个非常奇怪的问题。
我将一些.zip文件下载到/ mnt / sdcard / Download。
调用BroadcastReceiver.onReceive(),并检查下载的文件是否存在返回true。
然而,过了一会儿文件消失了。
虽然我不删除它。设置了权限WRITE_EXTERNAL_STORAGE 。
任何想法可能导致此问题或如何修复它??
编辑:这是代码:
public class DownloadUtil {
private DownloadTask task = null;
private long downloadQueueId;
private DownloadManager downloadManager;
private DownloadCallback callback = null;
public DownloadUtil(DownloadCallback callback) {
this.callback = callback;
}
public interface DownloadCallback {
public void onDownloadComplete(String filename);
}
/**
* Call this to download a file.
*/
public void downloadFile(String address, String destName) {
if (task != null) {
return;
}
task = new DownloadTask(address, destName);
task.execute((Void) null);
}
/**
* Represents an asynchronous login/registration task used to download.
*/
public class DownloadTask extends AsyncTask<Void, Void, Boolean> {
String address;
String destName;
public DownloadTask(String address, String destName) {
this.address = address;
this.destName = destName;
}
@Override
protected Boolean doInBackground(Void... params) {
boolean retVal = true;
try {
download(address, destName);
} catch (Exception e1) {
retVal = false;
e1.printStackTrace();
Log.e("DownloadUtil.java", "Exception downloading: ", e1);
}
return retVal;
}
@Override
protected void onPostExecute(final Boolean success) {
task = null;
if (success) {
// finish();
} else {
}
}
@Override
protected void onCancelled() {
task = null;
}
}
/**
* Performs the actual download.
*/
private boolean download(String address, String destName) throws ClientProtocolException, IOException {
boolean retVal = false;
BroadcastReceiver onDownloadComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
Query query = new Query();
query.setFilterById(downloadQueueId);
Cursor c = downloadManager.query(query);
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
Log.i("DownloadUtil.java", "File downloaded to: "+uriString);
uriString = uriString.substring(7);
File f = new File(uriString);
Log.i("DownloadUtil.java", "File: "+uriString+" exits: "+f.exists());
callback.onDownloadComplete(uriString);
}
}
TabLayoutActivity.context.unregisterReceiver(this);
}
}
};
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(address));
request.setDescription("Download Request");
request.setTitle("Download");
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, destName);
downloadManager = (DownloadManager) TabLayoutActivity.context.getSystemService(Context.DOWNLOAD_SERVICE);
TabLayoutActivity.context.registerReceiver(onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
downloadQueueId = downloadManager.enqueue(request);
return retVal;
}
}
编辑2:此行为是否可能是由远程服务器引起的?我刚刚注意到,如果我从我的一台服务器下载完全相同的文件,代码运行良好。仅当从客户服务器下载文件时,它似乎才会引起问题。
答案 0 :(得分:2)
答案 1 :(得分:1)
是的,这与@Artjom列出的问题有关。 DownloadManager中有一个错误,它有时会(通常是?)触发已完成下载的另一个下载请求。 ACTION_DOWNLOAD_COMPLETE也是广播,但由于后续下载失败,它会删除下载条目并另外删除本地文件。
我通过检查存在(+大小)并将文件移动到另一个位置来解决此问题。