Android 11 更新破坏了我的文件编写代码

标签 android android-file android-11

我之前用这个 fragment 写了一个简单的文本文件,其中包含名称和正文:

public static File writeTextFile(String sFileName, String sBody) throws IOException {
        File root = new File(Environment.getExternalStorageDirectory(), FOLDER_NAME);

    if (!root.exists()) {
        logFile("Not yet created, creating right now: " + root.getPath());
        root.mkdirs();

        logFile("Creation was successful? " + (root.exists() ? "Yes." : "No."));

    } else {
        logFile("Path exists: " + root.getPath());
    }

  
    File       measurementFile = new File(root, sFileName);
    FileWriter writer  = new FileWriter(measurementFile);
    writer.append(sBody);
    writer.flush();
    writer.close();

    return measurementFile;

}

它运行良好Android 11 除外,这是我今天在运行 Android 11 的 OnePlus N10 上对其进行测试后才意识到的。


问题是我没有收到任何描述的错误消息,只是一个简单的异常,并且在出现此问题后我似乎找不到继续的方法。

 java.io.FileNotFoundException: /storage/emulated/0/example_myapp/content_file_to_upload_1635424827184: open failed: ENOENT (No such file or directory)
     at libcore.io.IoBridge.open(IoBridge.java:492)
     at java.io.FileOutputStream.<init>(FileOutputStream.java:236)
     at java.io.FileOutputStream.<init>(FileOutputStream.java:186)
     at java.io.FileWriter.<init>(FileWriter.java:90)
     at com.myapp.example.util.FileUtil.writeTextFile(FileUtil.java:39)
     at com.myapp.example.services.ConnectionServiceForDownloadData$1.onCharacteristicChanged(ConnectionServiceForDownloadData.java:265)
     at android.bluetooth.BluetoothGatt$1$8.run(BluetoothGatt.java:478)
     at android.bluetooth.BluetoothGatt.runOrQueueCallback(BluetoothGatt.java:780)
     at android.bluetooth.BluetoothGatt.access$200(BluetoothGatt.java:41)
     at android.bluetooth.BluetoothGatt$1.onNotify(BluetoothGatt.java:472)
     at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:306)
     at android.os.Binder.execTransactInternal(Binder.java:1170)
     at android.os.Binder.execTransact(Binder.java:1134)
 Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
     at libcore.io.Linux.open(Native Method)
     at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
     at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
     at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7924)
     at libcore.io.IoBridge.open(IoBridge.java:478)
    ... 12 more

非常感谢任何帮助。

最佳答案

File root = new File(Environment.getExternalStorageDirectory(), FOLDER_NAME);

更改为:

File root = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), FOLDER_NAME);

关于Android 11 更新破坏了我的文件编写代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69754319/

相关文章:

java - Java翻译成 "private lateinit var lookupKey: String"是什么

Android:proc文件夹中的无限循环

Android 保存文件权限被拒绝

Android 11 (R) 文件路径访问

android - 如何创建文件夹(Android R - Api 30)?

android - 在 System.getenv 中找不到 Github Secret

java - 如何在 WebView 中保存和加载 HTML 图片?

android - 如何以编程方式调用电话?

java - 重命名android中的连续文件夹和文件

android - intent.resolveActivity 在 API 30 中返回 null