Can't write file on SD-card

前端 未结 1 1530
执笔经年
执笔经年 2021-01-16 15:35

I\'m trying to write a photo on the SD card without success.

I\'ve got the permissions to write in the removable storage and the sd card is mounted.

Also

相关标签:
1条回答
  • 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)

    0 讨论(0)
提交回复
热议问题