KitKat, Storage Access Framework, Specific File Pe

2019-09-03 16:23发布

问题:

I have an app that implements its own interface for selecting files, which is a requirement for the app to work properly.

In KitKat, files on an (External) SD-Card need to have permission given via the Storage Access Framework before they can be modified.

Since these restricted files are still freely readable, it doesn't stop my app from displaying them. But once the user selects to perform an operation on one of these files can I prompt the user with a specific permission request using the Storage Access Framework?

Below is the sample from the documentation. This returns a set of all possible images. Obviously I don't want to show the user all images, if I already can identify the specific image that I'm requesting permission for. Are there any other filters that would narrow down what's presented to the user?

(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");

回答1:

for ACTION_OPEN_DOCUMENT probably you need FLAG_GRANT_PERSISTABLE_URI_PERMISSION

check the documentation:

https://developer.android.com/reference/android/content/Intent.html#ACTION_OPEN_DOCUMENT



回答2:

Unfortunately SAF in KitKat doesn't have that advanced capability.

Depending on what you need, starting API 24 you can ask user to grant you permission to manage files in SD card by calling https://developer.android.com/reference/android/os/storage/StorageVolume.html#createAccessIntent(java.lang.String).

In API 21 - 23 you can direct user to grant you the entire SD card by invoking https://developer.android.com/reference/android/content/Intent.html#ACTION_OPEN_DOCUMENT_TREE.

That will grant you access through SAF to the sdcard, and you can leverage SAF to show/modify contents in SD card easily.