Reputation: 23
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
Reputation: 1006674
The ideal solution, for many apps, is to use methods on Context
for storing content:
getFilesDir()
or getCacheDir()
getExternalFilesDir()
or getExternalCacheDir()
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:
MediaStore
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 contentUpvotes: 1