Home > OS >  Android 11 update broke my File writing code
Android 11 update broke my File writing code

Time:10-29

I've used this snippet before to write a simple text file with a name and a body:

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;

}

It is working well except for Android 11 which I just realized today after testing it on a OnePlus N10 which runs Android 11.


The problem is I don't get any described error message just a simple exception and I can't seem to find a way to go on after this problem has occurred.

 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

Any help is greatly appreciated.

CodePudding user response:

Try adding android:requestLegacyExternalStorage="true" to your manifest.

Or change it to comply to Android 11's new scoped storage system.
See https://developer.android.com/about/versions/11/privacy/storage

CodePudding user response:

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

Change to:

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