英文:
Creating a Text File in Android results in FileNotFoundException
问题
我正在开发一个应用程序,该应用程序通过BLE获取数据并将其保存在文本文件中。
目前的问题是,我无法在我的应用程序中创建文本文件。
我已经尝试过这个帖子中的答案:https://stackoverflow.com/questions/8152125/how-to-create-text-file-and-insert-data-to-that-file-on-android
所以请不要将其标记为重复,因为那里提供的答案对我不起作用。
相应的代码如下:
try {
System.out.println(Environment.getExternalStorageState());
File root = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "BLE_Crawler");
if (!root.exists()) {
root.mkdirs();
}
File file = new File(root, fileName + ".txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter writer = new FileWriter(file, true);
writer.append("test");
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
我得到的错误是:
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Documents/BLE_Crawler/test.txt:打开失败:ENOENT(没有该文件或目录)
似乎我没有权限创建新文件夹,因为"mkdirs()"这一行不起作用。但我不知道如何解决这个问题。
哦,我在我的AndroidManifest中添加了以下内容:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
我正在使用SDK版本29。
英文:
I'm working on an app that gets data over BLE and saves it in a text file.
The problem now is that I'm not able to create a text file with my app.
I've already tried the answers from this thread: https://stackoverflow.com/questions/8152125/how-to-create-text-file-and-insert-data-to-that-file-on-android
So please don't mark it as duplicated, cause the answers provided there won't work for me..
The corresponding code is:
try {
System.out.println(Environment.getExternalStorageState());
File root =new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "BLE_Crawler");
if (!root.exists()) {
root.mkdirs();
}
File file = new File(root, fileName +".txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter writer = new FileWriter(file, true);
writer.append("test");
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
And the error I get is:
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Documents/BLE_Crawler/test.txt: open failed: ENOENT (No such file or directory)
It seems that I don't have the rights to make a new folder cause the "mkdirs()" line doesn't work. But I don't know how to fix this problem.
Oh and I have
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
in my AndroidManifest.
I'm using SDKVersion 29.
答案1
得分: 1
你尝试过在 <application>
标签中的清单中添加:
android:requestLegacyExternalStorage="true"
另外,不要忘记,从 Android 6.0 开始,即使在清单中写了权限,你仍需要请求权限。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String[] permissions = new String[]{READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE};
for (int i = 0; i < permissions.length; i++) {
if (checkSelfPermission(permissions[i]) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(permissions,
REQUEST_PERMISSIONS);
return;
}
}
}
英文:
Have you tried adding:
android:requestLegacyExternalStorage="true"
to your manifest in <application>
tag?
Also, don't forget that from Android 6.0 you need to request permissions even if you write them in your Manifest.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String[] permissions = new String[]{READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE};
for (int i = 0; i < permissions.length; i++) {
if (checkSelfPermission(permissions[i]) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(permissions,
REQUEST_PERMISSIONS);
return;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论