Android 11 更新破坏了我的文件编写代码
Android 11 update broke my File writing code
我之前使用此代码段编写一个简单的文本文件,其中包含名称和正文:
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.onCharacteristicChanged(ConnectionServiceForDownloadData.java:265)
at android.bluetooth.BluetoothGatt.run(BluetoothGatt.java:478)
at android.bluetooth.BluetoothGatt.runOrQueueCallback(BluetoothGatt.java:780)
at android.bluetooth.BluetoothGatt.access0(BluetoothGatt.java:41)
at android.bluetooth.BluetoothGatt.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
非常感谢任何帮助。
尝试将 android:requestLegacyExternalStorage="true"
添加到您的清单中。
或更改它以符合 Android 11 的新范围存储系统。
参见 https://developer.android.com/about/versions/11/privacy/storage
File root = new File(Environment.getExternalStorageDirectory(), FOLDER_NAME);
更改为:
File root = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), FOLDER_NAME);
我之前使用此代码段编写一个简单的文本文件,其中包含名称和正文:
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.onCharacteristicChanged(ConnectionServiceForDownloadData.java:265)
at android.bluetooth.BluetoothGatt.run(BluetoothGatt.java:478)
at android.bluetooth.BluetoothGatt.runOrQueueCallback(BluetoothGatt.java:780)
at android.bluetooth.BluetoothGatt.access0(BluetoothGatt.java:41)
at android.bluetooth.BluetoothGatt.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
非常感谢任何帮助。
尝试将 android:requestLegacyExternalStorage="true"
添加到您的清单中。
或更改它以符合 Android 11 的新范围存储系统。
参见 https://developer.android.com/about/versions/11/privacy/storage
File root = new File(Environment.getExternalStorageDirectory(), FOLDER_NAME);
更改为:
File root = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), FOLDER_NAME);