简体   繁体   English

兼容通过android版本下载一个PDF

[英]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. 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:我保存 PDF 的旧方法包括使用:

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为了使它工作,即使设备没有我正在使用的 sd 卡

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??但是现在,如果我被迫使用 SKD=30,android:requestLegacyExternalStorage 设置为 false,那么,如何调整应用程序来管理 PDF 从 minSdkVersion 19 下载到没有目标 SDkVersion 30 的设备? 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.如果版本> = Q,我管理了如何下载PDF,但是如何处理android 6,7,8,9 ... 对于我的应用程序,设置用户权限ZC31B32364CE19CA8FCD150A417ECCE58STORAGE的方法被拒绝,因为不是有效的。不是核心应用程序。

The ideal solution, for many apps, is to use methods on Context for storing content:对于许多应用程序来说,理想的解决方案是使用Context上的方法来存储内容:

  • For stuff that the user does not need independent access to, use things like getFilesDir() or getCacheDir()对于用户不需要独立访问的内容,请使用getFilesDir()getCacheDir()
  • For stuff that the user might need access to via USB cable, use things like getExternalFilesDir() or getExternalCacheDir()对于用户可能需要通过 USB 电缆访问的内容,请使用getExternalFilesDir()getExternalCacheDir()
  • For stuff that the user might want on removable storage, use things like getExternalFilesDirs() (note the plural)对于用户可能想要在可移动存储上的东西,使用getExternalFilesDirs()之类的东西(注意复数)

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对于媒体,使用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使用存储访问框架,例如ACTION_CREATE_DOCUMENT ,允许用户决定在用户设备(或用户的云存储)上放置内容的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM