【发布时间】:2019-09-18 08:52:59
【问题描述】:
我正在尝试写入 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>
【问题讨论】:
标签: android