且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用为Android 5.0(Lollipop)提供的新SD卡访问API?

更新时间:2023-02-24 19:59:27

很多好的问题,让我们深入探讨.:)

Lots of good questions, let's dig in. :)

这是与KitKat中的存储访问框架进行交互的出色教程:

Here's a great tutorial for interacting with the Storage Access Framework in KitKat:

https://developer.android.com/guide/topic/providers/document-provider.html#client

与Lollipop中的新API交互非常相似.要提示用户选择目录树,您可以启动这样的意图:

Interacting with the new APIs in Lollipop is very similar. To prompt the user to pick a directory tree, you can launch an intent like this:

    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
    startActivityForResult(intent, 42);

然后在onActivityResult()中,可以将用户选择的Uri传递给新的DocumentFile帮助器类.这是一个简短的示例,列出了所选目录中的文件,然后创建了一个新文件:

Then in your onActivityResult(), you can pass the user-picked Uri to the new DocumentFile helper class. Here's a quick example that lists the files in the picked directory, and then creates a new file:

public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    if (resultCode == RESULT_OK) {
        Uri treeUri = resultData.getData();
        DocumentFile pickedDir = DocumentFile.fromTreeUri(this, treeUri);

        // List all existing files inside picked directory
        for (DocumentFile file : pickedDir.listFiles()) {
            Log.d(TAG, "Found file " + file.getName() + " with size " + file.length());
        }

        // Create a new file and write into it
        DocumentFile newFile = pickedDir.createFile("text/plain", "My Novel");
        OutputStream out = getContentResolver().openOutputStream(newFile.getUri());
        out.write("A long time ago...".getBytes());
        out.close();
    }
}

DocumentFile.getUri()返回的Uri具有足够的灵活性,可以与可能不同的平台API一起使用.例如,您可以使用Intent.setData()Intent.FLAG_GRANT_READ_URI_PERMISSION共享它.

The Uri returned by DocumentFile.getUri() is flexible enough to use with may different platform APIs. For example, you could share it using Intent.setData() with Intent.FLAG_GRANT_READ_URI_PERMISSION.

如果要从本机代码访问该Uri,可以调用ContentResolver.openFileDescriptor(),然后使用ParcelFileDescriptor.getFd()detachFd()获得传统的POSIX文件描述符整数.

If you want to access that Uri from native code, you can call ContentResolver.openFileDescriptor() and then use ParcelFileDescriptor.getFd() or detachFd() to obtain a traditional POSIX file descriptor integer.

默认情况下,通过Storage Access Framework意图返回的Uris在重新启动后不会保留.该平台提供"了保留权限的能力,但是如果需要,您仍然需要获取"该权限.在上面的示例中,您将调用:

By default, the Uris returned through Storage Access Frameworks intents are not persisted across reboots. The platform "offers" the ability to persist the permission, but you still need to "take" the permission if you want it. In our example above, you'd call:

    getContentResolver().takePersistableUriPermission(treeUri,
            Intent.FLAG_GRANT_READ_URI_PERMISSION |
            Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

您始终可以找出通过ContentResolver.getPersistedUriPermissions() API可以持久授予您的应用程序访问权限的内容.如果您不再需要访问保留的Uri,则可以使用ContentResolver.releasePersistableUriPermission()释放它.

You can always figure out what persisted grants your app has access to through the ContentResolver.getPersistedUriPermissions() API. If you no longer need access to a persisted Uri, you can release it with ContentResolver.releasePersistableUriPermission().

否,我们无法向旧版本的平台追溯添加新功能.

No, we can't retroactively add new functionality to older versions of the platform.

当前没有显示此内容的用户界面,但是您可以在adb shell dumpsys activity providers输出的授予的Uri权限"部分中找到详细信息.

There's currently no UI that shows this, but you can find the details in the "Granted Uri Permissions" section of adb shell dumpsys activity providers output.

Uri权限授予是按用户隔离的.也就是说,在两个不同用户下运行的同一应用程序没有重叠或共享的Uri权限授予.

Uri permission grants are isolated on a per-user basis, just like all other multi-user platform functionality. That is, the same app running under two different users has no overlaping or shared Uri permission grants.

支持的DocumentProvider可以随时撤消权限,例如在删除基于云的文档时.发现这些已撤销权限的最常见方法是,当它们从上述ContentResolver.getPersistedUriPermissions()中消失时.

The backing DocumentProvider can revoke permission at any time, such as when a cloud-based document is deleted. The most common way to discover these revoked permissions is when they disappear from ContentResolver.getPersistedUriPermissions() mentioned above.

每当涉及授权的任何一个应用程序的数据被清除时,权限也会被撤销.

Permissions are also revoked whenever app data is cleared for either app involved in the grant.

是的,ACTION_OPEN_DOCUMENT_TREE意图使您可以递归访问现有和新创建的文件和目录.

Yep, the ACTION_OPEN_DOCUMENT_TREE intent gives you recursive access to both existing and newly created files and directories.

是的,自KitKat开始支持多种选择,您可以通过在启动ACTION_OPEN_DOCUMENT意图时设置EXTRA_ALLOW_MULTIPLE来允许选择.您可以使用Intent.setType()EXTRA_MIME_TYPES缩小可以选择的文件类型:

Yep, multiple selection has been supported since KitKat, and you can allow it by setting EXTRA_ALLOW_MULTIPLE when starting your ACTION_OPEN_DOCUMENT intent. You can use Intent.setType() or EXTRA_MIME_TYPES to narrow the types of files that can be picked:

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

是的,主要的共享存储设备应该出现在选择器中,即使在模拟器上也是如此.如果您的应用仅使用Storage Access Framework来访问共享存储,则完全不需要READ/WRITE_EXTERNAL_STORAGE权限 ,可以删除它们或使用android:maxSdkVersion功能仅在较旧的平台上请求它们版本.

Yep, the primary shared storage device should appear in the picker, even on the emulator. If your app only uses the Storage Access Framework for accessing shared storage, you no longer need the READ/WRITE_EXTERNAL_STORAGE permissions at all and can remove them or use the android:maxSdkVersion feature to only request them on older platform versions.

涉及物理介质时,基础介质的UUID(例如FAT序列号)始终会刻录到返回的Uri中.即使用户在多个插槽之间交换媒体,系统也会使用它将您连接到用户最初选择的媒体.

When physical media is involved, the UUID (such as FAT serial number) of the underlying media is always burned into the returned Uri. The system uses this to connect you to the media that the user originally selected, even if the user swaps the media around between multiple slots.

如果用户交换第二张卡,则需要提示您获得对新卡的访问权限.由于系统会根据每个UUID记住赠款,因此,如果用户稍后重新插入原始卡,您将继续拥有先前授予的原始卡访问权限.

If the user swaps in a second card, you'll need to prompt to gain access to the new card. Since the system remembers grants on a per-UUID basis, you'll continue to have previously-granted access to the original card if the user reinserts it later.

http://en.wikipedia.org/wiki/Volume_serial_number