RonRadtke / react-native-blob-util

A project committed to making file access and data transfer easier, efficient for React Native developers.
MIT License
772 stars 132 forks source link

Android: Proper way to download a file for user media consumption #380

Open PaperbagWriter opened 2 months ago

PaperbagWriter commented 2 months ago

Versions:

react-native-blob-util: "0.19.11", react-native: "0.73.9"

I'm confused about what is the best way to download a file on Android on different versions and for this media to be accessible in the Download apps or the Gallery apps. For the regular downloads internally to the app everything I think is pretty straight forward but for the above use case, the doc is very ambiguous.

From the doc I got the impression that from Android version 10 and upwards we should use copyToMediaStore. This works but makes it so that the downloadManager won't point to the file upon completion (downloads to cache then copies), so I need to skip the download manager.

For the versions below 10 we should use a path such as ReactNativeBlobUtil.fs.dirs.LegacyDCIMDir.

My confusion comes from the fact that I'm able to download to ReactNativeBlobUtil.fs.dirs.LegacyDCIMDir without using the copyToMediaStore on all Android versions (9-13). Is there a reason why I should not do that (and it is considered legacy)?

This also avoids having unwanted cached files, downloading directly to the internal storage InternalStorage/DCIM.

The documentation is pretty non-existent for Android below 10 so I wanted to update it, but because of the confusion, I did not.

Thanks

RonRadtke commented 1 month ago

It all boils down to what you want your user to do. ReactNativeBlobUtil.fs.dirs.LegacyDCIMDir is private to your app. So it will not show int he users gallery, or downloads for the other paths. If you want the file to be visible in the gallery / downloads folder, you have to use the media store api on Android > 10. For Android < 10 you can download the file to the downloaddir, or you use the Android Downloadmanager.

The android downloadmanager can also be used for Android > 10. But the API for it to add files to the downloads folder is deprecated, so unsure how long we can use it.