Android写入文件权限被拒绝

时间:2019-04-29 17:34:31

标签: android

我正在尝试写入android中的文件,但我不断收到此错误:

java.io.FileNotFoundException:/storage/emulated/0/Notes/TestFile.txt(权限被拒绝)

搜索此站点可为此问题提供简单的解决方案,该解决方案是将WRITE_EXTERNAL_STORAGE添加到应用清单文件中,但这对我不起作用。我不断收到相同的错误。

要写入文件的代码:

try {
                File root = new File(Environment.getExternalStorageDirectory(), "Notes");
                if (!root.exists()) {
                    root.mkdirs();
                }
                File gpxfile = new File(root, "TestFile.txt");
                FileWriter writer = new FileWriter(gpxfile);
                writer.append("Hello World");
                writer.flush();
                writer.close();
                Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                e.printStackTrace();
            }

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>

3 个答案:

答案 0 :(得分:1)

您在AndroidManifest.xml中提到了权限,但尚未得到用户的授予。您需要在WRITE_EXTERNAL_STORAGE活动中通过用户授予Android权限。

if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }

下次启动Activity时,请授予其权限,然后您就可以访问外部存储。请注意,这将仅提供写访问权限,而不会提供读访问权限。

答案 1 :(得分:1)

如果目标sdk等于或大于mashmallow版本,则必须要求运行时权限

您可以在这里查看

https://developer.android.com/training/permissions/requesting

答案 2 :(得分:0)

修复后,我不得不使用我在写入文件上方的代码请求权限:

            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    1);

            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    1);

完整的代码如下所示,可通过按钮单击功能写入文件:

          ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    1);

            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    1);




            try {
                File root = new File(Environment.getExternalStorageDirectory(), "Notes");
                if (!root.exists()) {
                    root.mkdirs();
                }
                File gpxfile = new File(root, "TestFile.txt");
                FileWriter writer = new FileWriter(gpxfile);
                writer.append("Hello World");
                writer.flush();
                writer.close();
                Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                e.printStackTrace();
            }