存储访问框架,takePersistableUriPermission

时间:2016-10-21 08:38:28

标签: android storage

在我的应用程序中,用户可以选择下载目录。如果他选择外部可移动 SD卡(不是模拟SD卡!,而是内存,例如真正的物理microSD卡),从Android 4.4开始,我只能使用它写入SAF(存储访问框架)。

我已经想出如何使用SAF创建和编写单个文件:

public void newFile() {
    Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);

    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_TITLE, "newfile.txt");

    startActivityForResult(intent, CREATE_REQUEST_CODE);
}

public void saveFile() {
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("text/plain");

    startActivityForResult(intent, SAVE_REQUEST_CODE);
}

这是我的onActivityResult,我实际写到文件:

public void onActivityResult(int requestCode, int resultCode,
                             Intent resultData) {
    Uri currentUri = null;

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == CREATE_REQUEST_CODE) {
            if (resultData != null) {
                Log.d(TAG, "CREATE_REQUEST_CODE resultData = " + resultData);
            }
        } else if (requestCode == SAVE_REQUEST_CODE) {

            if (resultData != null) {
                currentUri = resultData.getData();
                getContentResolver().takePersistableUriPermission(currentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION
                        | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                writeFileContent(currentUri);
                Log.d(TAG, "SAVE_REQUEST_CODE currentUri = " + currentUri);
            }
        }
    }
}

还有writeFileContent

private void writeFileContent(Uri uri) {
    try {
        ParcelFileDescriptor pfd =
                this.getContentResolver().
                        openFileDescriptor(uri, "w");

        FileOutputStream fileOutputStream =
                new FileOutputStream(pfd.getFileDescriptor());

        String textContent = "some text";

        fileOutputStream.write(textContent.getBytes());

        fileOutputStream.close();
        pfd.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

最后我的问题是:

如何创建其他文件,并在我调用getContentResolver().takePersistableUriPermission之后编写它们,而不提示将来选择目录?

如果我是对的,那么getContentResolver().takePersistableUriPermission允许我做那个

1 个答案:

答案 0 :(得分:1)

感谢@ earthw0rmjim的回答和谷歌搜索,我想出了一个完整的解决方案:

public void saveFile() {
    List<UriPermission> permissions = getContentResolver().getPersistedUriPermissions();
    if (permissions != null && permissions.size() > 0) {
        DocumentFile pickedDir = DocumentFile.fromTreeUri(this, permissions.get(0).getUri());
        DocumentFile file = pickedDir.createFile("text/plain", "try2.txt");
        writeFileContent(file.getUri());
    } else {
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("text/plain");

        startActivityForResult(intent, SAVE_REQUEST_CODE);
    }
}

首先,如果我们没有PersistedUriPermissions,它会请求此类权限。这种方式就是onActivityResult的样子

public void onActivityResult(int requestCode, int resultCode,
                             Intent resultData) {
    Uri currentUri = null;

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_DIR_REQUEST_CODE) {
            if (resultData != null) {
                Uri treeUri=resultData.getData();
                Log.d(TAG, "SELECT_DIR_REQUEST_CODE resultData = " + resultData);
                getContentResolver().takePersistableUriPermission(treeUri, Intent.FLAG_GRANT_READ_URI_PERMISSION
                        | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                DocumentFile pickedDir = DocumentFile.fromTreeUri(this, treeUri);
                DocumentFile file = pickedDir.createFile("text/plain", "try2.txt");
                writeFileContent(file.getUri());
            }
        }
    }
}

writeFileContent与问题

中的相同
private void writeFileContent(Uri uri) {
    try {
        ParcelFileDescriptor pfd =
                this.getContentResolver().
                        openFileDescriptor(uri, "w");

        FileOutputStream fileOutputStream =
                new FileOutputStream(pfd.getFileDescriptor());

        String textContent = "some text";

        fileOutputStream.write(textContent.getBytes());

        fileOutputStream.close();
        pfd.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}