Why don't I have permission to write to app dir on external storage?

前端 未结 2 990
余生分开走
余生分开走 2021-02-04 02:53

The TL;DR question summary: My Android app tries to write to the app\'s external storage directory on an SD card. It fails with a permi

2条回答
  •  名媛妹妹
    2021-02-04 03:35

    I've learned more about this issue, and it's different enough from CommonsWare's answer that I think it's worth a new answer.

    • What made the difference for my original scenario was the SD cards: If a card already had a /Android/data/com.example.myapp folder on it that was not created on this phone, then the app might have trouble getting permission to write to that folder. Whereas if the folder did not exist, the app could create it, and write to it. At least in KitKat and later.
      • This is supported by something explained in this article:

        ... starting with API Level 19 [KitKat], READ_EXTERNAL_STORAGE was no longer required to access files located on external storage – provided the data folder created by the FUSE daemon matches the app’s package name. FUSE would handle synthesizing the owner, group, and modes of files on external storage when an application is installed [emphasis added].

      • So this confirms the guess that the problem arose because I created the app's data folder manually, instead of letting Android set it up as it needed. Future research: Instead of looking only to app permissions like WRITE_EXTERNAL_STORAGE, also check the user, group, and mode settings of the app's data folder on the filesystem: both when created manually, and when created by app installation. Compare & contrast! Maybe this will provide enough information to allow the app's data folder to be created manually and still work. Keep in mind that there is a wrapper / emulation layer over the FAT32 filesystem of the SD card, so we need to consider both filesystem layers.
    • In a later scenario, I found that it's necessary for the app to call context.getExternalFilesDirs(null) in order for the /Android/data/com.example.myapp folder to be created on the SD card. (At least, on Android 5.1 Lollipop and later. Need to test that on KitKat.)

提交回复
热议问题