I am trying to upgrade my targetSdkVersion
to 30. A download manager in the legacy code, expects a File
object and creates a file at the file path. I was creating the file using this syntax : File("${Environment.getExternalStorageDirectory()}/$directoryPath")
.
Since the introduction of Scoped Storage
, i can no longer do so.
According to official document document:
- I can either create the file in my app directory
- Or I can add the file to
Shared Storage
(which is the preferred use case for me)
Method 2 suggest use of Storage Access Framework
. But for that i will have to replace the Download Manager. And that will be huge task and lot for refactoring.
However, i came across this answer on SO
which suggests using File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString()+File.separator+directoryPath)
and strangely, it works on both android 10 and 11.
Wondering how it works even though it is deprecated, and is it the right way of doing things?