以下代码在较低的API-29中效果很好。但是使用API-29时会出现此错误。
public void persistFile(byte[] bytes, String path, String fileName) throws IOException {
OutputStream fOut = null;
File picDirectory = new File(path);
File file = new File(path, fileName);
fOut = new FileOutputStream(file);
fOut.write(bytes);
fOut.flush();
fOut.close();
}
可以在清单应用程序标签中使用添加android:requestLegacyExternalStorage="true"
。
<manifest ... >
<!-- This attribute is "false" by default on apps targeting
Android 10 or higher. -->
<application android:requestLegacyExternalStorage="true" ... >
...
</application>
</manifest>