BananaBuster
BananaBuster

Reputation: 71

DownloadManager double download

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.

Upvotes: 6

Views: 3521

Answers (2)

AndreDLeite
AndreDLeite

Reputation: 1

I've got the same error on mobile devices with API 21, I've made a workaround to verify before creating a request, if the file name used to set de request destination was equal one of the last files already downloaded, or if its a substring of any previews downloaded

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
}

and where "outputFile" is the file name itself to be downloaded, in your case this should be "filename.extension"

PS: Sorry for the Kotlin code, but it should be a good representation for the workaround itself

Upvotes: 0

jakub
jakub

Reputation: 3844

Unfortunately, DownloadManager is buggy and doesn't work correctly on all devices. Your problem is reported here: https://code.google.com/p/android/issues/detail?id=18462

Upvotes: 2

Related Questions