从ACTION_OPEN_DOCUMENT_TREE根Uri为Android 5.0 DocumentFile构造Uri

时间:2015-11-25 05:48:52

标签: android uri documentfile

我可以从Storage Access Framework从OPEN_DOCUMENT_TREE成功获取基路径Uri。

How to use the new SD card access API presented for Android 5.0 (Lollipop)?

private static final int READ_REQUEST_CODE = 42;

public void performFileSearch() {

    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);

    startActivityForResult(intent, READ_REQUEST_CODE);
}


@Override
public void onActivityResult(int requestCode, int resultCode,
                             Intent resultData) {

    // The ACTION_OPEN_DOCUMENT intent was sent with the request code
    // READ_REQUEST_CODE. If the request code seen here doesn't match, it's the
    // response to some other intent, and the code below shouldn't run at all.

    if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        // The document selected by the user won't be returned in the intent.
        // Instead, a URI to that document will be contained in the return intent
        // provided to this method as a parameter.
        // Pull that URI using resultData.getData().
        Uri uri = null;
        if (resultData != null) {
            uri = resultData.getData();

        }
    }
}

但是在Android 5.0中有一个错误/功能会破坏递归,如本文所述:

Bug when listing files with Android Storage Access framework on Lollipop

Uri treeUri = resultData.getData();
DocumentFile pickedDir = DocumentFile.fromTreeUri(this, treeUri);
Uri f1 = pickedDir.findFile("MyFolder").getUri();
Log.d(TAG, "f1 = " + f1.toString());

使用File.listFiles()返回Null数组。

我已经知道目标文件夹/文件的完整路径。我想构建一个有效的DocumentFile Uri,它具有onActivityResult中返回的根Uri的权限。

我想附加到根Uri路径或构建一个新的Uri,它具有与根Uri相同的权限来访问目标文件夹/文件。

1 个答案:

答案 0 :(得分:0)

你基本上想要切片和切块uri的路径段。您还希望避免调用findFile。它的性能与文件夹大小负相关。数百个文件可能意味着多秒,并且它会继续上升。

我的解决方案是使用正常运行的getParent包装DocumentFile。我还没有完成(即:这段代码功能不完全),但它可能会指出如何操纵uris达到你的目标。

/**
 *  Uri-based DocumentFile do not support parent at all
 *  Try to garner the logical parent through the uri itself
 * @return
 */
protected UsefulDocumentFile getParentDocument()
{
    Uri uri = mDocument.getUri();
    String documentId = DocumentsContract.getDocumentId(uri);

    String[] parts = getPathSegments(documentId);

    if (parts == null)
        return null;

    Uri parentUri;
    if (parts.length == 1)
    {
        String parentId = DocumentsContract.getTreeDocumentId(uri);
        parentUri = DocumentsContract.buildTreeDocumentUri(uri.getAuthority(), parentId);
    }
    else
    {
        String[] parentParts = Arrays.copyOfRange(parts, 0, parts.length - 2);
        String parentId = TextUtils.join(URL_SLASH, parentParts);
        parentUri = DocumentsContract.buildTreeDocumentUri(uri.getAuthority(), parentId);
    }

    return UsefulDocumentFile.fromUri(mContext, parentUri);
}

这再一次没有完全发挥作用,但它可能会指向正确的方向。当我弄清楚所有的问题时,我会更新。