0

I'm trying to write to a file in android but i keep getting this error:

java.io.FileNotFoundException: /storage/emulated/0/Notes/TestFile.txt (Permission denied)

Searching this site gives a simple resolution to this problem, which is to add WRITE_EXTERNAL_STORAGE to the app manifest file, but this isnt working for me. I keep getting the same error.

Code to write to file:

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>
Some_Dude
  • 309
  • 5
  • 21

3 Answers3

1

You have mentioned permission in AndroidManifest.xml but not yet granted it from the user. You need to grant WRITE_EXTERNAL_STORAGE permission through the user in your Android activity.

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

Next time you launch the Activity, grant it the permission and then you can access external storage. Mind that this will give only write access, not read access.

Sushant Somani
  • 1,450
  • 3
  • 13
  • 31
  • Im getting an error with this code: "error: incompatible type. cannot be converted to Context" and "error: incompatible type. cannot be converted to Activity". Any idea what i can replace "this" with to make it work in my onClick function? I found this thread which is similar, but doesnt fix my issue. https://stackoverflow.com/questions/27704006/error-using-onclicklistener-intent – Some_Dude Apr 29 '19 at 18:05
  • 1
    Yes you can replace 'this' with getContext() or getApplicationContext() – Sushant Somani Apr 30 '19 at 00:47
1

If your targeted sdk is equal or greater to mashmallow version you must ask for runtime permissions

You can check it here

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

0

Fixed it, i had to request permission using this code just above where i wrote to the file:

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

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

The full code looks like this to write to a file in a button click function:

          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();
            }
Some_Dude
  • 309
  • 5
  • 21