The following codes take a String and save the content to an existing file Uri. The codes were working well in Android pre API 29.
public void saveFile(String text, Uri existingSourceUri)
{
try {
ContentResolver cr = getContentResolver();
OutputStream os = cr.openOutputStream(existingSourceUri);
os.write(text.getBytes());
os.flush();
os.close();
} catch (Exception e) {
//show error message
}
}
With Android API 29+, the behavior is erratic. For example, if the function is called the first time with some text, the file is properly saved. However, if the second time text is blank, the file is not saved.
Any help?
cr.openOutputStream(existingSourceUri, "wt");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With