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

问题描述

以下代码采用String并将内容保存到现有文件Uri中。这些代码在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
    }
}

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

有帮助吗?

解决方法

cr.openOutputStream(existingSourceUri,"wt");

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...