Android:覆盖 API 29 中现有 URI 的文件

Kam*_*mil 4 android uri outputstream file android-contentresolver

以下代码获取 aString并将内容保存到现有文件中Uri。这些代码在 Android 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
    }
}
Run Code Online (Sandbox Code Playgroud)

对于 Android API 29+,行为不稳定。例如,如果第一次使用 some 调用该函数text,则该文件将被正确保存。但是,如果第二次text为空,则不会保存文件。

有什么帮助吗?

bla*_*pps 11

cr.openOutputStream(existingSourceUri, "wt");
Run Code Online (Sandbox Code Playgroud)

  • 您能详细说明一下“t”代表什么吗?我读到它的意思是“截断”(https://developer.android.com/reference/android/content/ContentProvider#openFile(android.net.Uri,%20java.lang.String)),但我不确定是什么这意味着 (3认同)