Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: overwrite file from an existing URI in API 29

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?

like image 666
Kamil Avatar asked Sep 03 '25 07:09

Kamil


1 Answers

cr.openOutputStream(existingSourceUri, "wt");
like image 132
blackapps Avatar answered Sep 04 '25 21:09

blackapps