java - Android 10 不保存文本文件
问题描述
我生成了一个文本文件并尝试保存它。它不仅适用于 android Q。在 android 9 、 8 和 7 上它有效。当我检查文件是否存在于 android 10 上时,它可以工作,但我不在设备上。我这样做:
private void createFile(Context ctx, String fileName, String text) {
File filesDir = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
filesDir = ctx.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
}
assert filesDir != null;
if (!filesDir.exists()){
if(filesDir.mkdirs()){
}
}
File file = new File(filesDir, fileName + ".txt");
try {
if (!file.exists()) {
if (!file.createNewFile()) {
throw new IOException("Cant able to create file");
}
}
OutputStream os = new FileOutputStream(file);
byte[] data = text.getBytes();
os.write(data);
os.flush();
os.close();
Log.e("TAG", "File Path= " + file.toString());
if(file.exists()){
Log.e("d","d");
}
File fileTemp = new File(file.getPath());
FileInputStream notes_xml = new FileInputStream(fileTemp);
byte fileContent[] = new byte[(int)notes_xml.available()];
//The information will be content on the buffer.
notes_xml.read(fileContent);
String strContent = new String(fileContent);
Log.e("content",strContent);
notes_xml.close();
} catch (IOException e) {
e.printStackTrace();
}
}
解决方案
推荐阅读
- javascript - unity3d webgl 在选项卡或浏览器不活动时静音
- firebase - firebase 函数配置 - 删除数据
- java - 从复杂的服务器响应中提取字段
- javascript - 如何将连续数字作为字符串推送到 json 对象中?
- angular - 使用异步管道时缓存发出的值以便能够过滤掉未来的发出
- python - PyTorch Tensorboard 与文档中的描述不符
- apache-spark - Spark+Mesos Initial job 没有接受任何资源;检查您的集群 UI 以确保工作人员已注册并拥有足够的资源
- sql - SQL 查询以确定是否有任何与曲目无关的艺术家
- sql - SQL : 获取相关属性
- javascript - JQuery 表不会加载。,无法加载资源