在Android中创建文本文件会导致FileNotFoundException。

huangapple go评论123阅读模式
英文:

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
所以请不要将其标记为重复,因为那里提供的答案对我不起作用。

相应的代码如下:

  1. try {
  2. System.out.println(Environment.getExternalStorageState());
  3. File root = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "BLE_Crawler");
  4. if (!root.exists()) {
  5. root.mkdirs();
  6. }
  7. File file = new File(root, fileName + ".txt");
  8. if (!file.exists()) {
  9. file.createNewFile();
  10. }
  11. FileWriter writer = new FileWriter(file, true);
  12. writer.append("test");
  13. writer.flush();
  14. writer.close();
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }

我得到的错误是:

  1. W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Documents/BLE_Crawler/test.txt打开失败ENOENT没有该文件或目录

似乎我没有权限创建新文件夹,因为"mkdirs()"这一行不起作用。但我不知道如何解决这个问题。

哦,我在我的AndroidManifest中添加了以下内容:

  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  2. <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:

  1. try {
  2. System.out.println(Environment.getExternalStorageState());
  3. File root =new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), &quot;BLE_Crawler&quot;);
  4. if (!root.exists()) {
  5. root.mkdirs();
  6. }
  7. File file = new File(root, fileName +&quot;.txt&quot;);
  8. if (!file.exists()) {
  9. file.createNewFile();
  10. }
  11. FileWriter writer = new FileWriter(file, true);
  12. writer.append(&quot;test&quot;);
  13. writer.flush();
  14. writer.close();
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }

And the error I get is:

  1. 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

  1. &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; /&gt;
  2. &lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot; /&gt;

in my AndroidManifest.

I'm using SDKVersion 29.

答案1

得分: 1

你尝试过在 <application> 标签中的清单中添加:

  1. android:requestLegacyExternalStorage="true"

另外,不要忘记,从 Android 6.0 开始,即使在清单中写了权限,你仍需要请求权限。

  1. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  2. String[] permissions = new String[]{READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE};
  3. for (int i = 0; i < permissions.length; i++) {
  4. if (checkSelfPermission(permissions[i]) != PackageManager.PERMISSION_GRANTED) {
  5. requestPermissions(permissions,
  6. REQUEST_PERMISSIONS);
  7. return;
  8. }
  9. }
  10. }
英文:

Have you tried adding:

  1. android:requestLegacyExternalStorage=&quot;true&quot;

to your manifest in &lt;application&gt; tag?
Also, don't forget that from Android 6.0 you need to request permissions even if you write them in your Manifest.

  1. if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
  2. String[] permissions = new String[]{READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE};
  3. for (int i = 0; i &lt; permissions.length; i++) {
  4. if (checkSelfPermission(permissions[i]) != PackageManager.PERMISSION_GRANTED) {
  5. requestPermissions(permissions,
  6. REQUEST_PERMISSIONS);
  7. return;
  8. }
  9. }
  10. }

huangapple
  • 本文由 发表于 2020年10月22日 20:58:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64482747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定