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

前端 未结 2 986
余生分开走
余生分开走 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.)
    0 讨论(0)
  • 2021-02-04 03:57

    Since one app worked and another didn't, the difference was between the apps, not with the device or card. The directory in question does not require any Android permissions (e.g., WRITE_EXTERNAL_STORAGE). The only reason why you would not be able to write to it would be if the Android system had not set up the filesystem permissions properly.

    I may have created that directory myself, and failed to set up permissions somewhere?

    I am not certain that you can create that directory yourself from outside the app and have it work. Ideally that would be fine, but I have not tried it and I can see where that might pose problems.

    Is there another reason not to trust it?

    Given the filesystem shenanigans that are going on, I get very nervous when developers make assumptions regarding the nature of paths, that's all.

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