При возобновлении скачивания видеофайл закачан в android - PullRequest
0 голосов
/ 28 апреля 2020

Я загружаю видеофайл, он работает нормально и воспроизводится после успешной загрузки, но если я приостанавливаю, а затем возобновляю загрузку, он успешно загружает оставшийся файл, но он поврежден и не воспроизводит видео

            Request.Builder builder = new Request.Builder();
            builder = builder.url(downloadFileUrl);

            builder = builder.addHeader("RANGE", "bytes=" + existLocalFileLength);
            Request request = builder.build();

            Call call = okHttpClient.newCall(request);
            Response response = call.execute();

            if (response != null && response.isSuccessful()) {
                RandomAccessFile downloadFile = new RandomAccessFile(existLocalFile, "rw");

                downloadFile.seek(existLocalFileLength);
                ResponseBody responseBody = response.body();
                InputStream inputStream = responseBody.byteStream();
                BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
                byte data[] = new byte[102400];
                long totalReadLength = 0;
                int readLength = bufferedInputStream.read(data);
                while (readLength != -1) {

                    if (getDownloadManager().isDownloadPaused()) {
                        ret = DOWNLOAD_PAUSED;
                        break;
                    } else if (getDownloadManager().isDownloadCanceled()) {
                        ret = DOWNLOAD_CANCELED;
                        break;
                    } else {

                        downloadFile.write(data, 0, readLength);

                        totalReadLength = totalReadLength + readLength;

                        int downloadProgress = (int) ((totalReadLength + existLocalFileLength) * 100 / downloadFileLength);

                        Log.d("download_progress", "download_progress..." + downloadProgress);                

                        }

                        readLength = bufferedInputStream.read(data);
                    }
                }

1 Ответ

0 голосов
/ 28 апреля 2020

Вы использовали BufferedInputStream , чтобы возобновить его. Вы должны использовать DownloadManager.Request , вот пример.

private void startDownloading(String link) {    
            Uri uri = Uri.parse(link);
            downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
            DownloadManager.Request request = new DownloadManager.Request(uri);
            request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
                    DownloadManager.Request.NETWORK_MOBILE);
            request.setTitle(createPDFPath());
            request.allowScanningByMediaScanner();
            request.setDestinationInExternalPublicDir( Environment.DIRECTORY_DOWNLOADS, createPDFPath());
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setMimeType("application/mp4");
            reference_id = downloadManager.enqueue(request);    
    }
...