Filedownloader: Accept DocumentFile to support direct download to External SD Card

Created on 6 Oct 2016  ·  10Comments  ·  Source: lingochamp/FileDownloader

To write directly to the SD Card on Android 6+ we have to use the Access Framework, but it changes the File to DocumentFile, can you add the option to pass a custom DocumentFile, or instead the outputStream from the DocumentFile?

enhancement top task

Most helpful comment

I face the same problem. But here are few things to note:

  1. This issue is about physical removable memory card, like microSD, which is inserted into a phone (android device), not about the emulated SD card, which is a part of phone's built-in memory.
  2. Issue comes on Android 4.4 devices and up (in API 19 google restricted direct access to memory cards.), not from Android 6
  3. For my oppinion, acceptable destenation type for the download can be Uri, which can be created from File, from DocumentFile. So it would be grait to replace or just add setPath(String path) with setUri(Uri uri) in BaseDownloadTask.

I've tried to do it myself, but the library source code is too complex for me.

Here is how to open OutputStream from Uri:

Uri uri = ...;
ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "w");
FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());

All 10 comments

I face the same problem. But here are few things to note:

  1. This issue is about physical removable memory card, like microSD, which is inserted into a phone (android device), not about the emulated SD card, which is a part of phone's built-in memory.
  2. Issue comes on Android 4.4 devices and up (in API 19 google restricted direct access to memory cards.), not from Android 6
  3. For my oppinion, acceptable destenation type for the download can be Uri, which can be created from File, from DocumentFile. So it would be grait to replace or just add setPath(String path) with setUri(Uri uri) in BaseDownloadTask.

I've tried to do it myself, but the library source code is too complex for me.

Here is how to open OutputStream from Uri:

Uri uri = ...;
ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "w");
FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());

I have no issues with SD in Api 19

I did that method in one part of my app, but the FileDescriptor closes while writing, i found other method to do the transfer:

Uri uri = ...;
OutputStream outputstream=getContentResolver().openOutputStream(uri, "rw");

Sorry for my missed this issue, this is very significant.

Did anyone manage to have a workaround while this feature is being developed? I tried creating a class that implements FileDownloadOutputStream but that only takes in a File in the Creator constructor

Sorry guys, I will handle this in the next version.

Any plan to release 1.4.2?

Sorry for delay, I tried to write code to support this feature, but it's not free time enough time for me, so all processing is intercepted.

On the 1.5.8, it will be implemented with:

  1. implement Uri OutputStream
  2. change the .temp file mechanism to .lock file mechanism, which means we no longer using the temp file since we have no access permission to its .temp file, and we store its .lock file to the application folder and make the mapping to the target file path if the .lock file exists the file is not completed downloading, so we can feel free to pre-allocate for the target file with write data to the target file directly.
  3. The old .temp file migration: I will try to migrate the .temp file, but if there isn't permission to access the old .temp file, it will be discarded directly.

Above is the imagine plan, any advice for this feature is open... please comment and let me know.

Please do this. :+1:

@Jacksgong When can we expect this ? Thank you for amazing library

This issue has been solved on okdownload.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Jeffin21 picture Jeffin21  ·  4Comments

seransaca picture seransaca  ·  5Comments

itsmallsun picture itsmallsun  ·  5Comments

techGay picture techGay  ·  3Comments

asdf19910719 picture asdf19910719  ·  4Comments