smanikandan14 / ThinDownloadManager

To download files and to avoid using DOWNLOAD_WITHOUT_NOTIFICATION permission if you are using Android's DownloadManager in your apps.
Apache License 2.0
783 stars 185 forks source link

File downloaded no fully #106

Open Rehckover opened 7 years ago

Rehckover commented 7 years ago

I try to dowload file with size 3.7MB and often receives onComplete while actual file size is 122kb or 50kb. Can you help with this? My link is: https://doc-0g-4k-docs.googleusercontent.com/docs/securesc/f6a1d72cge9p0eq8mkjrughdsskulo18/gqbs46o1bts8v2mn4sv312fk30oavn7s/1503914400000/04284872070643305544/04284872070643305544/0B6e1WFHf4jrKdXFiQ0d6dEZ3WEE?e=download

My code is:

   public void downloadFile(String url) {
        Uri downloadUri = Uri.parse(url);
        File destinationFolder = context.getExternalCacheDir();
        assert destinationFolder != null;
        Uri destinationUri = Uri.parse(destinationFolder.getAbsolutePath()
                + "/"
                + "depth.apk");
        DownloadRequest downloadRequest = new DownloadRequest(downloadUri)
                .setRetryPolicy(new DefaultRetryPolicy())
                .setDestinationURI(destinationUri).setPriority(DownloadRequest.Priority.HIGH)
                .setStatusListener(new DownloadStatusListenerV1() {
                    @Override
                    public void onDownloadComplete(DownloadRequest downloadRequest) {
                        Log.d(TAG, "onDownloadComplete: ");
                    }

                    @Override
                    public void onDownloadFailed(DownloadRequest downloadRequest, int errorCode, String errorMessage) {
                        Log.d(TAG, "onDownloadFailed: " + errorCode + " : " + errorMessage);
                    }

                    @Override
                    public void onProgress(DownloadRequest downloadRequest, long totalBytes, long downloadedBytes, int progress) {
                    }
                });
        manager.add(downloadRequest);
    }
jhysum commented 4 years ago

I get this as well `private void transferData(DownloadRequest request, InputStream in, RandomAccessFile out) { final byte data[] = new byte[BUFFER_SIZE]; long mCurrentBytes = mDownloadedCacheSize; request.setDownloadState(DownloadManager.STATUS_RUNNING); Timber.v("Content Length: " + mContentLength + " for Download Id " + request.getDownloadId()); for (; ; ) { if (request.isCancelled()) { Timber.v(STOPPING_THE_DOWNLOAD_AS_DOWNLOAD_REQUEST_IS_CANCELLED, request.getDownloadId()); request.finish(); updateDownloadFailed(request, DownloadManager.ERROR_DOWNLOAD_CANCELLED, DOWNLOAD_CANCELLED); return; } int bytesRead = readFromResponse(request, data, in);

        if (mContentLength != -1 && mContentLength > 0) {
            int progress = (int) ((mCurrentBytes * 100) / mContentLength);
            updateDownloadProgress(request, progress, mCurrentBytes);
        }

        if (bytesRead == -1) { // success, end of stream already reached
            updateDownloadComplete(request);
            return;
        } else if (bytesRead == Integer.MIN_VALUE) {
            return;
        }

        if (writeDataToDestination(request, data, bytesRead, out)) {
            mCurrentBytes += bytesRead;
        } else {
            request.finish();
            updateDownloadFailed(request, DownloadManager.ERROR_FILE_ERROR, "Failed writing file");
            return;
        }
    }
}`

I think in here we get to bytesRead == -1 somehow. I haven't started debugging this yet but I get this sometimes.