提问者:小点点

Android写入文件权限被拒绝[重复]


我试图在android中写入文件,但一直出现以下错误:

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

搜索此网站可以简单地解决此问题,即向应用程序清单文件添加WRITE\u EXTERNAL\u存储,但这对我不起作用。我一直在犯同样的错误。

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();
            }
<?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个答案

匿名用户

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

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

下次启动active时,授予它权限,然后您可以访问外部存储。请注意,这将只授予写访问权限,而不是读访问权限。

匿名用户

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

你可以在这里查看

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

匿名用户

修复了它,我必须在我写入文件的地方使用此代码请求权限:

            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();
            }