我按照以下方法修改 但还是没法写入sdcard 提示No such file or directory
来源:4-3 单例封装时间转换类

旧日星光
2021-07-17
小米10
在AndroidManifest文件中添加了
<application
android:requestLegacyExternalStorage=“true”
使用了最新的logUtils文件
public static void writeToFile(String text) {
//开始写入
FileOutputStream fileOutputStream = null;
BufferedWriter bufferedWriter = null;
try {
//文件路径
String fileRoot = Environment.getExternalStorageDirectory().getPath() + "/Meet/";
String fileName = "Meet.log";
// 时间 + 内容
String log = mSimpleDateFormat.format(new Date()) + " " + text + "\n";
//检查父路径
File fileGroup = new File(fileRoot);
//创建根布局
if (!fileGroup.exists()) {
fileGroup.mkdirs();
}
//创建文件
File fileChild = new File(fileRoot + fileName);
if (!fileChild.exists()) {
fileChild.createNewFile();
}
fileOutputStream = new FileOutputStream(fileRoot + fileName, true);
//编码问题 GBK 正确的存入中文
bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, Charset.forName("gbk")));
bufferedWriter.write(log);
} catch (FileNotFoundException e) {
e.printStackTrace();
e(e.toString());
} catch (IOException e) {
e.printStackTrace();
e(e.toString());
} finally {
if (bufferedWriter != null) {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
写回答
1回答
-
旧日星光
提问者
2021-07-22
已解决 添加动态权限
012023-01-07