10

In Android 4.4, Apps from Play Store can write only to it's App specific directory( eg:/storage/extSdCar/Android/data/com.example.myapp/ ) and apps are not allowed to write other than this directory in micro SD card. So I am exploring the new SAF API to check whether I can use SAF to write to micro SD card other than the app specific directory.

I have implemented SAF by creating a sample provider and client app. In my provider, I tried to show the entire content of SD card by implementing the following code in queryRoots:

    row.add(Root.COLUMN_DOCUMENT_ID, getDocIdForFile(new File("/storage/extSdCard")));
    row.add(Root.COLUMN_FLAGS, Root.FLAG_SUPPORTS_CREATE | Root.FLAG_SUPPORTS_RECENTS | Root.FLAG_SUPPORTS_SEARCH);

I have created the following intent in my client and I am able to browse the entire content of SD card through my provider from System UI of SAF.

    Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("*/*");
    intent.putExtra(Intent.EXTRA_TITLE, "test.txt");
    startActivityForResult(intent, WRITE_REQUEST_CODE);

When I click the save button,I am getting a Toast error "Failed to save document".But when I navigate to the App specific directory(/storage/extSdCar/Android/data/com.example.myprovider/) of my provider in System UI, I am able to save the document. Also there is NO intent such as Intent.CATEGORY_WRITABLE so that I can get only documents that can be edited. Please help me on the below points:

1.It seems that even if we use SAF, still WRITE restriction on micro SD card remains the same. Is this correct?

2.Is there any way that I can create or delete or modify already available files outside my App specific directory in micro SD card?

3.Who decides the FLAG_SUPPORTS_WRITE flag for normal user files such as Photos, Videos, Music, documents, etc ?

1 Answers1

4

When implementing a DocumentsProvider, you can only extend access to files on disk that you already have access to. Adding a DocumentsProvider doesn't give you any new access, so you probably don't need to implement one.

You can gain write access anywhere on an SD card by using the ACTION_OPEN_DOCUMENT or ACTION_CREATE_DOCUMENT intents that you mentioned. There is an ExternalStorageProvider built into the system that has access to all SD cards, and it will gladly delegate write access to you once confirmed by the user through those intents.

Users may need to enable "Settings > Display advanced devices" to show the SD cards offered by ExternalStorageProvider. Also, if you want to limit the user to interacting with local storage, you can set EXTRA_LOCAL_ONLY.

As for (2), you can create new files outside your package-specific directory with ACTION_CREATE_DOCUMENT, and you can select existing files with either intent. User-selected files can be deleted with DocumentsContract.deleteDocument().

And for (3), MediaDocumentsProvider built into the platform currently does not extend FLAG_SUPPORTS_WRITE for Photos, Videos, and Music.

Hope this helps. :)

Jeff Sharkey
  • 2,473
  • 1
  • 17
  • 10
  • 1
    Thanks for the reply. Is there any way to gain WRITE access to files located in micro SD card without launching System UI of SAF? how can Apps like File Manager edit or delete a particular file in micro SD card without launching System UI? Since System UI is again like a File browser for files, how can a File Manager app do WRITE/ DELETE operation on a particular file located in micro SD card ? – Sivaraj Velusamy Apr 02 '14 at 18:08
  • Since the Storage Access Framework mediates access to potentially sensitive user data, it currently requires user confirmation before granting write access. Is the File Manager app you mention something provided by an OEM? Built-in apps may use additional permissions to access devices directly. – Jeff Sharkey Apr 02 '14 at 19:18
  • 1
    No. It is not an app provided by OEM. It is a 3rd party app available in Play Store. It is still unclear to me Why this restriction is implemented only on microSd card and NOT on the internal storage. There will be "potentially sensitive user data" in the Internal memory also and I wonder why this restriction is not applied to Internal memory. Do we see any advantage by imposing WRITE restriction only on micro SD card? – Sivaraj Velusamy Apr 03 '14 at 10:56
  • The primary shared storage on an Android device has been available to apps holding WRITE_EXTERNAL_STORAGE since API 1, so we can't remove that functionality. If a device was configured so that a physical SD card slot was the primary shared storage, apps with WRITE_EXTERNAL_STORAGE could write anywhere on that media. (This device configuration varies based on the OEM.) – Jeff Sharkey Apr 03 '14 at 22:05
  • sorry Jeff. still I didn't get the answer for my question.What advantage are we getting by imposing different set of permission for primary and secondary storage ? If you say that it gives advantage in terms of SECURITY, then it makes the data stored in PRIMARY storage vulnerable to security attacks, rgt? As a developer, I can clearly see that there is some inconsistency in the deign of KitKat regarding this issue.Do you agree? If not, please explain it in detail. Note: It is a well known fact that most of the OEMs make Internal Embedded memory as PRIMARY and the micro SD as SECONDARY storage. – Sivaraj Velusamy Apr 04 '14 at 08:06
  • It would appear that the "inconsistency" is that that legacy considerations require that the primary ExternalStorage be directly accessible with a simple manifest permission. I read the design decisions behind the abstracted interface to secondary/removable storage as how it might *all* be done if it were being designed from scratch today, in a world where Android has moved (understandably) towards mistrust of 3rd party developers, but unfortunately *away from* the full potential of its heritage as a pocket *nix box that happened to be able to make phone calls ;-) – Chris Stratton Apr 21 '15 at 17:24