2

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

4

2 回答 2

1

是的,这与@Artjom 列出的问题有关。DownloadManager 中存在一个错误,它有时会(经常?)为已完成的下载触发另一个下载请求。ACTION_DOWNLOAD_COMPLETE 也是广播的,但是因为后续下载失败,所以删除了下载入口,另外删除了本地文件。

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

于 2013-11-20T23:11:40.377 回答