下载管理器:新下载的文件会自行消失



我在使用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:此行为可能是由远程服务器引起的吗?我刚刚注意到,如果我从我的一台服务器下载完全相同的文件,代码运行良好。似乎只有在从客户服务器下载文件时才会导致问题。

检查此问题:

问题 18462:Android 下载管理器下载同一文件两次,在其他文件上失败(错误 1008,ERROR_CANNOT_RESUME)

是的,这与@Artjom列出的问题有关。DownloadManager中有一个错误,它有时会(通常是?)为已经完成的下载触发另一个下载请求。该ACTION_DOWNLOAD_COMPLETE也会广播,但由于后续下载失败,它会删除下载条目并另外删除本地文件。

我已经通过检查是否存在(+大小)并将文件移动到另一个位置来解决此问题。

相关内容

  • 没有找到相关文章

最新更新