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

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

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), &quot;BLE_Crawler&quot;);
		if (!root.exists()) {
			root.mkdirs();

		}
		File file = new File(root, fileName +&quot;.txt&quot;);
		if (!file.exists()) {

				file.createNewFile();

		}
		FileWriter writer = new FileWriter(file, true);
		writer.append(&quot;test&quot;);
		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

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

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=&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.

if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
        String[] permissions = new String[]{READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE};
        for (int i = 0; i &lt; permissions.length; i++) {
            if (checkSelfPermission(permissions[i]) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(permissions,
                        REQUEST_PERMISSIONS);
                return;
            }
        }
    }

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:

确定