1

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.

Ayush Jain
  • 144
  • 10
  • Download to internal storage first, then move the completed result to removable storage when the download is complete. Or, download to a location on removable storage that you can directly access (e.g., `getExternalCacheDirs()` 2nd entry), then move the completed result to its intended location when the download is complete. – CommonsWare Feb 03 '17 at 13:02
  • @CommonsWare I am bound to save the downloading files in sd card only. I am dealing with huge amount of data ~4GB. And many of my users faced low internal storage issues. Also, copying this amount of data might take more than 5 min. – Ayush Jain Feb 03 '17 at 13:08
  • `use FileOutputStream, which help me to resume the file in case downloading fails.`. Why not mention that you can open a FileOutputStream in append mode and that that possibility fails while using Document. – greenapps Feb 03 '17 at 17:08
  • @greenapps In order to open a file in writeable mode (API 21) I am bound to use `openOutputStream` which help to access the files in sd card in writeable mode. – Ayush Jain Feb 04 '17 at 08:40
  • Now you really talk nonsense. – greenapps Feb 04 '17 at 09:07

0 Answers0