0

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:

  1. I can either create the file in my app directory
  2. 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?

ankuranurag2
  • 2,300
  • 15
  • 30
  • I think that if you go with the legacy thing, sometime in the not so distant future google will issue one of those "until November 3rd, you must update your file handling or else you will not be able to update your app". So perhaps you can get away with it for a year or so (I'm just guessing at this point). – Razvan S. Jun 24 '21 at 16:06
  • 1
    `is it the right way of doing things?` I think so. Instead of public Download directory you can also use public Documents directory. And others. – blackapps Jun 24 '21 at 16:55

0 Answers0