如何以编程方式写入Android中的DocumentFile?

Dim*_*ims 4 android file documentfile

Android API 有DocumentFile. 这个类有canWrite()方法

假设我调用了这个方法并且它返回了true。还假设该对象代表“原始”文件。

现在我该如何做它所说的事情呢?

即,如何将“Hello world”文本写入该文件?

谢谢。

Com*_*are 5

即,如何将“Hello world”文本写入该文件?

它不一定是一个文件。

要写入由 that 标识的文档DocumentFile,请调用getUri()DocumentFile来获取该Uri文档。将其传递openOutputStream()ContentResolver. 然后,写入流、flush()流、close()流。基本上,一旦获得OutputStream,普通的 Java I/O 就会接管。

  • @Dims:只要任何给定的“File”对象要么指向文件,要么无效。相反,“Uri”可以指向很多东西。例如,“http://stackoverflow.com/questions/42043114/how-to-write-to-documentfile-in-android-programmatically/42043137”是一个有效的“Uri”。它不指向任何客户端设备上的文件。 (2认同)