问题描述
以下代码采用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");