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

為{幸葍}努か 提交于 2021-01-07 06:34:40

问题


I'm trying to save a text file using SAF (Storage Access Framework) but I can't control where it should be saved, I used this method from the Documentation as follows:

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);
}

Passing the root directory Uri as follows:

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

But whatever string I parse and even if I pass null as Uri, the framework UI always starts in Downloads.

I want to save the file in the root directory. also, I want to save it automatically without any user interaction.


回答1:


Quoting the documentation for EXTRA_INITIAL_URI:

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

A "document URI" is one that you obtained previously from ACTION_OPEN_DOCUMENT or ACTION_CREATE_DOCUMENT. A "tree URI" is one that you obtained previously from ACTION_OPEN_DOCUMENT_TREE. Uri.parse() is neither of those.



来源:https://stackoverflow.com/questions/65001666/cant-control-the-initial-directory-where-the-saf-ui-should-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!