Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does SAF(Storage Access Framework) solve the SD card WRITE issue in Android 4.4 (KitKat)?

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 ?

like image 245
Sivaraj Velusamy Avatar asked Mar 28 '14 07:03

Sivaraj Velusamy


People also ask

What is storage access framework in Android?

Android 4.4 (API level 19) introduces the Storage Access Framework (SAF). The SAF makes it simple for users to browse and open documents, images, and other files across all of their preferred document storage providers.

Can you run Android apps from SD card?

Android apps are installed to your device's internal storage by default. If you have a microSD card, you can move some of your currently installed apps over to the microSD card. This is not supported by all apps, however. In fact, many don't support this feature at all.

What is SD Card Manager for Android?

SD Card manager (File Manager) is a free tool which helps you to easily manage files and folder in SD card. Access system files and folders. Full root access for copy, delete, move and rename. SD Card manager also supports Google Drive and Dropbox.


1 Answers

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

like image 172
Jeff Sharkey Avatar answered Sep 26 '22 02:09

Jeff Sharkey