Create new file in the directory returned by Intent.ACTION_OPEN_DOCUMENT_TREE

后端 未结 1 1708
Happy的楠姐
Happy的楠姐 2021-01-14 03:59

In my App the user can choose a directory where to create an Excel file using the implicit intent ACTION_OPEN_DOCUMENT_TREE. However, the Uri returned in onActivityRes

1条回答
  •  -上瘾入骨i
    2021-01-14 04:20

    Step #1: Take the Uri that you get from ACTION_OPEN_DOCUMENT_TREE and pass it to DocumentFile.fromTreeUri().

    Step #2: Call createFile() on that DocumentFile to get a DocumentFile representing the child document.

    Step #3: Call getUri() on the DocumentFile that you created in Step #2.

    Step #4: Call openOutputStream() on a ContentResolver, passing in the Uri from Step #3, to get an OutputStream that you can use to write content. You get a ContentResolver by calling getContentResolver() on some Context, such as an Activity.

    See this blog post for more on using ACTION_OPEN_DOCUMENT_TREE.

    0 讨论(0)
提交回复
热议问题