Can't write file on SD-card

梦想与她 提交于 2019-12-01 12:15:29

On API Level 18 and below, your getStorageDirectories() will go through its else block. That block assumes:

  • that the SECONDARY_STORAGE environment variable exists... which is not required
  • that the SECONDARY_STORAGE contains a delimited list of directories... which is not required
  • that the list of directories matches removable storage options... which is not required
  • that you can work with those directories... which is not required

On API Level 19+, your getStorageDirectories() code will go through its if block. There, you start off fine, calling getExternalFilesDirs(). If that method returns 2+ items, the second and subsequent ones point to removable storage, and specifically point to places on removable storage where you can read and write. Then, your code assumes:

  • that the directory has an /Android path segment... which is not required
  • that the portion of the directory path preceding /Android represents a location in which you can create files and directories... which is never true

You do not have filesystem-level access to removable storage, except in the specific directories returned by methods like getExternalFilesDirs().

So, either:

  • Stick to the specific locations returned by getExternalFilesDirs(), or

  • Switch to using the Storage Access Framework, allowing the user to choose where to store content (which may or may not be removable storage)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!