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 ?
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. :)