6

I have the following problem: Whenever I download a file with the DownloadManager it is downloaded twice (saved in the fashion "filename.extension" and "filename-1.extension"). Here is my code:

public void download() {
        Request request = new Request(Uri.parse(_wrapper.getURL()));
        request.setTitle(getFileName(_wrapper.getURL()));
        request.setVisibleInDownloadsUi(false);
        request.setDestinationInExternalFilesDir(_context, null, "/" + getFileName(_wrapper.getURL()));

        _downloadID = _downloadManager.enqueue(request);
    }



    public BroadcastReceiver getDownloadFinishedBroadcastReceiver() {
        BroadcastReceiver receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context pContext, Intent pIntent) {
                String action = pIntent.getAction();
                if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                    Query query = new Query();
                    query.setFilterById(_downloadID);
                    Cursor cursor = _downloadManager.query(query);
                    if (cursor.moveToFirst()) {
                        File file = new File(ScruloidConstants.APPLICATION_DIRECTORY);
                        int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
                        if (status == DownloadManager.STATUS_SUCCESSFUL) {
                            String path = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
                            _wrapper.setFilePath(path);
                            _wrapper.setLastDownloaded(new Date());
                            if (_listener != null) {
                                _listener.onDownloadProjectTaskFinished(new TaskResult<ProjectWrapper>(_wrapper));
                            }
                        }
                        else if (status == DownloadManager.STATUS_FAILED) {
                            int reason = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_REASON));
                            DownloadFailedException ex = new DownloadFailedException(reason);
                            if (_listener != null) {
                                _listener.onDownloadProjectTaskFinished(new TaskResult<ProjectWrapper>(ex));
                            }
                        }
                    }
                }
            }
        };
        return receiver;
    }

The ProjectWrapper _wrapper is just a simple Class that holds data, no logic is done there. The _listener just displays on the callback method a little Toast message. I debugged my app to make shure the download() Method is invoked only once. I hope you can help me find the error.

4

2 回答 2

2

不幸的是,DownloadManager 有问题,不能在所有设备上正常工作。您的问题在这里报告:https ://code.google.com/p/android/issues/detail?id=18462

于 2013-03-01T09:29:44.430 回答
0

我在使用 API 21 的移动设备上遇到了同样的错误,我在创建请求之前做了一个变通方法来验证,如果用于设置请求目标的文件名与已下载的最后一个文件相同,或者它的下载的任何预览的子字符串

if (!mLastMediaDownloadedId.any { it.contains(outputFile.name) }) {
                mLastMediaDownloadedId.add(outputFile.name)
                val url =
                    AppConstants.AWS_MEDIA_BUCKET_PATH + scoutObjectType.endPoint() + "$scoutObjectId.png"

                val request = DownloadManager.Request(Uri.parse(url))
                    .setDestinationUri(Uri.fromFile(outputFile))
                    .setTitle("Downlading media")
                    .setDescription("Downloading image medias")
                    .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
                    .setAllowedOverRoaming(true)
                    .setAllowedOverMetered(true)

                val downloadId = it.enqueue(request)
                downloadIds.add(downloadId)
                downloadId
}

其中“outputFile”是要下载的文件名本身,在您的情况下应该是“filename.extension”

PS:对不起 Kotlin 代码,但它应该是解决方法本身的一个很好的代表

于 2019-10-29T17:09:39.810 回答