无法控制 SAF UI 应该启动的初始目录

can't control the initial directory where the SAF UI should start

我正在尝试使用 SAF(存储访问框架)保存文本文件,但我无法控制应将其保存在何处,我使用了 Documentation 中的此方法,如下所示:

private void createFile(Uri pickerInitialUri) {
    Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("text/*");
    intent.putExtra(Intent.EXTRA_TITLE, "new.txt");

    // Optionally, specify a URI for the directory that should be opened in
    // the system file picker when your app creates the document.
    intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri);


    startActivityForResult(intent, CREATE_FILE);
}

传递根目录Uri如下:

Uri.parse(Environment.getExternalStorageDirectory().toString())

但是无论我解析什么字符串,即使我将 null 作为 Uri 传递,框架 UI 总是从下载开始。

我想将文件保存在根目录下。另外,我想在没有任何用户交互的情况下自动保存它。

引用 the documentation for EXTRA_INITIAL_URI:

[The extra] should specify a document URI or a tree URI with document ID.

“文档 URI”是您之前从 ACTION_OPEN_DOCUMENTACTION_CREATE_DOCUMENT 获得的 URI。 “树 URI”是您之前从 ACTION_OPEN_DOCUMENT_TREE 获得的 URI。 Uri.parse() 两者都不是。