I am building a downloading manager to download files and save it to my sd card. For API>=23, I have to use FileOutputStream, which help me to resume the file in case downloading fails.
However, in API 21 & API 22 (Lollipop), one can't write in Sd card, but it can be done with the help of DocumentFile.
I am able to create files and folders in Lollipop but unable to resume downloading if the file is partially downloaded. My code is as follows:
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());
}
It is rewriting the current file. I am looking for handling append mode in DocumentFile. I have provide all the required permissions. Help me out here, thanks in advance.