How does storage access change on Android 6?

28,474

Solution 1

Let me answer Adoptable Storage Devices related questions:

  1. Suppose the user has chosen to use "Adoptable Storage Devices", what does it mean for the various functions that retrieve the paths of the app's files? For example : getFilesDir, getExternalFilesDir,... ? Would the oder of getExternalFilesDirs change because of it?

When user choose to use SD card as "Adoptable Storage Device" (Format as internal), it means now that SD card is available only as Internal Storage i.e. no SD card available to store downloaded files. There will be no order change in paths returned by the related methods. For example: getExternalFilesDir() will list only external storage path if user formatted his SD card as "Adoptable Storage Devices". SD card path will not be available.

  1. What happens to the files of the app when the user moves the app from/to the SD-card (using the "Adoptable Storage Devices") ? What about the app's files on the SD-card? Would they stay? Or would they move somewhere? For example, if the app has "file1.txt" on the SD-card, on path "/storage/extSdCard/Android/data/appPackageName", and it has a file "file2.txt" (or even the same name) on the primary external storage on path "/storage/emulated/0/Android/data/appPackageName". After switching, what would happen for those files? How would they merge into a single folder, if at all?

When ever user will choose his SD card as "Adoptable Storage Devices" then user need to format his SD card as internal storage using "Format as internal" option. Format means all the data/files stored on SD card will be erased. Similarly when user want to remove his SD card from "Adoptable Storage Devices" then user again have to format his SD card as portable storage using "Format as portable" option.

  1. When moving the app to the SD-card (using "Adoptable Storage Devices"), does it mean no internal storage will be used?

Yes, original internal storage will not be used. Only SD card storage will be used because after choosing SD card as "Adoptable Storage Devices". All the data/cache will be stored to SD card

Solution 2

To answer your question 5: under Android 7, all files in the "public data" area, /storage/emulated/0/ (or a manufacturer-specific location; it's the directory containing DCIM Downloads, etc.), are stored either entirely in the internal memory or entirely on the adopted SD card. After the initial format of an adopted SD card the user will be presented with an opportunity to "Migrate data" which will move all of /storage/emulated/0 to the SD card. At any time the user can also go to Settings / Storage, pick the storage area that does not currently hold the public data area, and the and clicking the (three vertical dots) icon at the upper right to bring up a menu with the "Migrate data" option.

It appears that there's no way for a user or application to force a particular public file to the SD card if an adopted card is in use; it's all or nothing. (The app's private storage will be in the internal storage or on the SD card based on whether or not the app has been moved to the SD card; there too it appears that the app will only ever be using one or the other, not both.)

Share:
28,474

Related videos on Youtube

android developer
Author by

android developer

Really like to develop Android apps & libraries on my spare time. Github website: https://github.com/AndroidDeveloperLB/ My spare time apps: https://play.google.com/store/apps/developer?id=AndroidDeveloperLB

Updated on July 09, 2022

Comments

  • android developer
    android developer almost 2 years

    Background

    Android had a lot of changes as to how to handle the SD-card and storage in general:

    • API 3 - you get all access, no permission needed
    • API 4-15 - you need to use WRITE_EXTERNAL_STORAGE, and you get all access.
    • API 16-18 - if you wish only to read, use READ_EXTERNAL_STORAGE
    • API 19-20 - you can't read or write to secondary external storage (SD-card), unless your app is a system app, or you have root.
    • API 21-22 - in order to access the SD-card, you need to ask the user for permission, and use the DocumentFile API instead of the File API. This raied a lot of questions, as I've written about here, here and here.

    Starting with API 23 (Android 6), things seem to change yet again...

    The problem

    For API 23, there are at least 2 things that are new and are storage-related :

    • "Adoptable Storage Devices" - The user can optionally make the SD-card as something that's like the primary external storage.
    • As part of the new permissions mechanism (requesting permissions at runtime), it seems that storage is also a permission the user needs to confirm. This is for both READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE

    Since there is no Android 6 device out there that has SD-card, and because the emulator itself doesn't really have the ability to use an SD-card, it's still impossible to know what's going on.

    The questions

    1. Will the SD-card get the access using the File-API instead of DocumentFile?

    2. If I want access to all external storage paths (including SD-card), does this mean I need to request this permissions twice: one for the primary external storage and one for the SD-card?

    3. Are files on the SD-card accessible in any way before the manual granting of the permission?

    4. Suppose the user has chosen to use "Adoptable Storage Devices", what does it mean for the various functions that retrieve the paths of the app's files? For example : getFilesDir, getExternalFilesDir,... ? Would the oder of getExternalFilesDirs change because of it?

    5. What happens to the files of the app when the user moves the app from/to the SD-card (using the "Adoptable Storage Devices") ? What about the app's files on the SD-card? Would they stay? Or would they move somewhere?

      For example, if the app has "file1.txt" on the SD-card, on path "/storage/extSdCard/Android/data/appPackageName", and it has a file "file2.txt" (or even the same name) on the primary external storage on path "/storage/emulated/0/Android/data/appPackageName". After switching, what would happen for those files? How would they merge into a single folder, if at all?

    6. When moving the app to the SD-card (using "Adoptable Storage Devices"), does it mean no internal storage will be used?

    • CommonsWare
      CommonsWare almost 9 years
      "Since there is no Android 6 device out there that has SD-card, and because the emulator itself doesn't really have the ability to use an SD-card, it's still impossible to know what's going on" -- quoting the documentation that you linked to, "To debug this feature in the developer preview, you can enable adoption of a USB drive that is connected to an Android device through a USB On-The-Go (OTG) cable". Worked fine on MNCv1.
    • CommonsWare
      CommonsWare almost 9 years
      "What happens to the files of the app when the user moves the app from/to the SD-card (using the "Adoptable Storage Devices")?" -- internal storage (e.g., getFilesDir()) will move to the adopted storage. There is no change to files on external storage, unless the docs and my experiments missed something. "What about the app's files on the SD-card?" -- the adopted storage is reformatted and turned into an encrypted volume; anything on it is obliterated as part of the adoption process.
    • android developer
      android developer almost 9 years
      About the emulator, this I know, but it's not like a real SD-card, as it's considered the primary external storage. You can only have a single external storage on the emulator. This situation is very very rare nowadays. All devices that I know about have both internal and external storage. None have only internal and SD-card, as done on the emulator. In fact, you can't set 0MB for the SD-card, just because it's the primary external storage.
    • android developer
      android developer almost 9 years
      About "Adoptable Storage Devices", does it mean nothing can be stored on the internal storage once it's done? I also assume the app is now stored on a different partition of the SD-card, right? One that no app can read from.
    • CommonsWare
      CommonsWare almost 9 years
      "does it mean nothing can be stored on the internal storage once it's done?" -- for an individual app, AFAIK, yes. As with the old Android 2.x-era "apps2SD" stuff, users can have some apps on internal storage and some on removable storage. "I also assume the app is now stored on a different partition of the SD-card, right?" -- as I wrote, the adopted storage is reformatted and turned into an encrypted volume. I don't have details on finer-grained security than that, though I would assume it is equivalent to internal storage with respect of apps getting at other apps' stuff.
    • CommonsWare
      CommonsWare almost 9 years
      "does it mean nothing can be stored on the internal storage once it's done?" -- however, all the APIs (e.g., getFilesDir()) still work. They just point elsewhere. In that respect, it is akin to secondary accounts, which we have had since Android 4.2. And it's why I pound on the table to get developers to stop hardcoding paths.
    • android developer
      android developer almost 9 years
      Makes sense. Please try to answer the rest of the questions. Also, why do you write it all in comments?
    • android developer
      android developer almost 9 years
      @VicJordan I don't know. I guess it's a similar thing, but not all questions are related to the "Adoptable Storage Devices"
    • Evan Carroll
      Evan Carroll over 8 years
      Just FYI Sony Xperia's will get Android 6 updates, and CM13 runs perfectly fine (wayy better than 12.x) on my S5.
  • nalitzis
    nalitzis over 8 years
    Similarly, if I have SD card as Internal Storage but I decide to install the app in the real-internal storage, would I be able to use the SD card to store app data? And if yes, which API should I use as I understand that getExternalFilesDir will not return any path in that case?
  • android developer
    android developer almost 6 years
    What would happen to the files I've mentioned on #5 ? It was said it will be formatted. Is it true? Also, you said it's all or nothing. This means all apps and their data will be moved to the sd-card ? What is left now for the internal storage to store? Just system app and their data, perhaps? And what happens if the user pulls out the sd-card?
  • cjs
    cjs almost 6 years
    I'm not entirely clear on what you're asking. If you format an SD card with files on it, those files are gone, yes. If you just take out the old card and replace it with a new one, the files are still on the old card but not accessible to your phone. Or did you find some way of putting two SD cards in your device?