1

我正在构建一个下载管理器来下载文件并将其保存到我的 SD 卡中。对于 API>=23,我必须使用FileOutputStream,它可以帮助我在下载失败时恢复文件。

但是,在 API 21 & API 22 (Lollipop) 中,不能写入 SD 卡,但可以在DocumentFile的帮助下完成。
我可以在 Lollipop 中创建文件和文件夹,但如果文件部分下载,则无法继续下载。我的代码如下:

if (file.exists()) {
                    downloaded = (int) file.length();
                    connection.setRequestProperty("Range", "bytes=" + (file.length()) + "-");

                        String s = file.getPath();
                        String s1 = getRealPathFromURI(documentFile.getUri());
                        s = s.replace(s1, "");
                        Uri uri2 = Uri.parse(documentFile.getUri().toString() + Uri.encode(s));
                        output = getContentResolver().openOutputStream(uri2);
                    } else {
                        connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
                        DocumentFile tempDocumentFile = documentFile.createFile("video/mp4", "temp/" + downloadsPaths.get(i));
                        output = getContentResolver().openOutputStream(tempDocumentFile.getUri());
                    }

它正在重写当前文件。我正在寻找处理 DocumentFile 中的附加模式。我已提供所有必需的权限。帮帮我,在此先感谢。

4

0 回答 0