Android 10:在外部存储设备上将文件保存到名为"/sdcard/my-app/"的目录时,我有哪些选择? [英] Android 10: What are my options to save files on external storage into a directory called "/sdcard/my-app/"

查看:828
本文介绍了Android 10:在外部存储设备上将文件保存到名为"/sdcard/my-app/"的目录时,我有哪些选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到Android Pie之前,我始终将应用程序需要存储的文件存储在/sdcard/my-app/上,这些文件是通过

Up until Android Pie I always stored files which the app needed to get stored on /sdcard/my-app/, which I got via

File fBaseDir = new File(Environment.getExternalStorageDirectory(), "my-app");

我的一个应用程序在/sdcard/my-app/favicons中存储了数百个(最多成千上万个)收藏夹图标,当用户创建该应用程序的备份时,它会存储在/sdcard/my-app/backups中.与我的应用共享时,Google Chrome浏览器拍摄的网页屏幕截图存储在/sdcard/my-app/screenshots中,依此类推.

One of my App stores hundreds (up to multiple thousands) of favicons in /sdcard/my-app/favicons, and when the user creates a backup of the app, it gets stored in /sdcard/my-app/backups. Webpage-Screenshots taken by Google Chrome when shared with my app are stored in /sdcard/my-app/screenshots, and so on.

当我卸载该应用程序时,我可以稍后重新安装它,但仍然可以使用所有图标,屏幕快照和备份来恢复状态,而不必重新下载所有图标(并且可能丢失了所有其他数据)

When I uninstall the app, I can reinstall it later and still have all the favicons, screenshots and the backups available for restoring the state and not having to re-download all the favicons (and possibly having lost all the other data).

我还可以通过将/sdcard/my-app/目录的备份轻松复制到PC上来创建备份,并在另一部手机上将其还原,例如,当我迁移到新设备时.

I can also easily create a backup of the /sdcard/my-app/ directory by copying it to my PC, and restore it on another phone, for example when I migrate to a new device.

我还可以使用文件浏览器查看该目录中的文件,然后选择并通过电子邮件发送或将其复制到Google云端硬盘,或专门删除其中的某些文件(例如旧的备份文件).

I can also use a file explorer to see which files are in that directory, and select and email them or copy them to Google Drive, or delete some of them specifically (like old backup files).

在Android 10上,这种方法已经失效.我无法将图标添加到Images Media-Folder,因为它们不是真实的图像,它们会不必要地使视图混乱,并有可能禁止我在Play商店中的开发者帐户执行此操作.我也不想将所有这些数据存储在App-Private目录中,因为在卸载该应用程序时这些数据将被删除,并且其他应用程序(例如资源管理器)也无法访问它们.

With Android 10 this approach has collapsed. I cannot add the favicons to the Images Media-Folder, because they are no real images, they would clutter the view unnecessarily and possibly ban my developer account from the Play Store for doing this. I also don't want to store all this data in an App-Private directory, because it will get deleted when the app gets uninstalled, and because other apps like an explorer cannot access them.

我有什么选择?除非要只选择一次目录然后永久授予访问权限(以读取和写入文件),否则不能手动选择文件或目录.

What are my options? Manually selecting files or directories is not an option, unless the directory only needs to be selected once and then access is granted forever (to read and write files).

我从不在/sdcard/my-app/之外读写.

推荐答案

在Android Q中,默认情况下,私有文件夹外的应用程序直接禁止文件访问.您可以根据自己的情况选择以下几种策略:

In Android Q direct File access is disabled by default for the apps outside their private folders. Here few strategies you can use in your case:

  1. 使用清单选项requestLegacyExternalStorage具有旧的行为,但在Android R上将不再起作用,因此这实际上是一个短期解决方案;
  2. 使用getExternalFilesDir()方法保存文件.这是您的私人文件夹,其他应用程序只有具有READ_EXTERNAL_STORAGE权限,才能访问这些文件.在这种情况下,最好使用FileProvider授予对您文件的其他应用程序的访问权限.
  3. 使用类StorageManager的方法getPrimaryStorageVolume().createOpenDocumentTreeIntent()来请求访问外部主卷.在这种情况下,您需要用户同意,并且无论如何都无法直接使用File api,但是使用DocumentFile类时,您将拥有非常相似的界面,因此,这是更接近于旧行为的解决方案.如果您需要在前台和后台执行操作,即无需用户交互(除请求请求权限的第一次交互),就可以使用它.
  1. Use the manifest option requestLegacyExternalStorage to have the old behavior but it won't work anymore with Android R, so it's really a short term solution;
  2. Save your files using getExternalFilesDir() method. It's your private folder, other apps could access these files only if they have READ_EXTERNAL_STORAGE permission. In this case it would be good to use FileProvider to grant access to other apps to your files.
  3. Use the method getPrimaryStorageVolume().createOpenDocumentTreeIntent() of class StorageManager to ask for access to the extenal primary volume. In this case you need user consent and you won't be able to use File api directly anyway, but using DocumentFile class you have a very similar interface, so this is the solution closer to the old behavior. It works if you need to perform operations in foreground and background, i.e. without user interaction with the exception the first interaction to request the permission.

我为第3点链接了 Flipper 库,它有助于管理文件(如旧版android).

I link Flipper library for point 3, it helps to manage files like in older android versions.

这篇关于Android 10:在外部存储设备上将文件保存到名为"/sdcard/my-app/"的目录时,我有哪些选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆