【问题标题】:Video File is Curropted on resume Downloading in android视频文件在恢复时损坏 在android中下载
【发布时间】:2020-08-11 21:15:18
【问题描述】:

我正在下载视频文件,它工作正常并在成功下载后播放,但如果我暂停然后继续下载,那么它会成功下载剩余的文件,但它已损坏且无法播放视频

            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);
                    }
                }

【问题讨论】:

    标签: java android file kotlin download


    【解决方案1】:

    你使用了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);    
        }
    

    【讨论】:

    • 我正在使用自定义下载管理器来下载与 netflix 相同的视频,而不是默认的 DownloadManager,因为我无法使用默认的 DownloadManager 暂停、恢复、取消和继续下载
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    • 2016-08-11
    相关资源
    最近更新 更多