【问题标题】:Android: overwrite file from an existing URI in API 29Android:从 API 29 中的现有 URI 覆盖文件
【发布时间】:2020-09-22 09:49:58
【问题描述】:

以下代码采用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 为空,则不会保存文件。

有什么帮助吗?

【问题讨论】:

  • if the second time text is blank, ?你的意思是text.isEmpty() 还是text.equals("")
  • @blackapps text.equals("")
  • 您认为会发生什么?第一次写“这很好”。第二次写“你好”。结果如何?
  • @blackapps hellois fine
  • 确实如此。而你希望它是什么?你现在明白 "" 什么都没做吗?

标签: android file uri outputstream android-contentresolver


【解决方案1】:
cr.openOutputStream(existingSourceUri, "wt");

【讨论】:

猜你喜欢
  • 2018-04-22
  • 1970-01-01
  • 2016-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多