Parris Person
Parris Person

Reputation: 23

Compatibility through android versions to download a PDF

in the next months the mandatory Android target SDK if you want to publish is 30, so im upgrading my Android project written in Java but I have a problem saving PDF from url. My old approach to save PDF consist in use:

SystemUtils.saveFile(pdfAsBytes, path.toString(), fileName + "." + this.fileType);

In order to make it work even if the device doesn't have sd card I'm using

android:requestLegacyExternalStorage="true"

But now, if i'm forced to use SKD=30, android:requestLegacyExternalStorage is setted to false, so, how is possible to adapt the app to manage PDF downloads from minSdkVersion 19 to targetSdkVersion 30 including devices with no SD card?? I managed how to download the PDF if version >= Q but how to deal with android 6,7,8,9... For my app, the approach of set user permission android.permission.MANAGE_EXTERNAL_STORAGE is not valid and was rejected because is not the core application.

Upvotes: 1

Views: 101

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006674

The ideal solution, for many apps, is to use methods on Context for storing content:

  • For stuff that the user does not need independent access to, use things like getFilesDir() or getCacheDir()
  • For stuff that the user might need access to via USB cable, use things like getExternalFilesDir() or getExternalCacheDir()
  • For stuff that the user might want on removable storage, use things like getExternalFilesDirs() (note the plural)

You have complete filesystem access within those directories, and you do not need to request any runtime permissions for them.

If you want to be able to write to shared public locations, your options are:

  • For media, use MediaStore
  • Use the Storage Access Framework, such as ACTION_CREATE_DOCUMENT, to allow the user to decide where on the user's device (or in the user's cloud storage) to place the content

Upvotes: 1

Related Questions