0

I want to open/create a document file and rewrite it completely if it exists.
Using How to rewrite DocumentFile does not help (at least in API 32). In fact,
OutputStream os = getContentResolver().openOutputStream(documentFile.getUri(), "w");
does not clear the rest of the file if the new stream is shorter.
Any way to resolve it?
Additional nuisance, DocumentFile paradigm seems incompatible with the regular Java File. There you can open a File, then check if it exists. On Android, if the doc file "name" exists, it creates "name (1)", "name (2)", etc. automatically, so it always exists.
Why??

Alex B
  • 347
  • 1
  • 3
  • 9

1 Answers1

0

Truncate by "wt".

If you use the same uri again then it will overwrite the same file.

Do not blindly call createFile.

First use findFile and if you get an uri then use that to overwrite.

blackapps
  • 8,011
  • 2
  • 11
  • 25