1

The following code output is "Write granted" but after that the error it '/storage/emulated/0/Download/test.txt: open failed: EACCES (Permission denied)'

What's wrong?

@RunWith(AndroidJUnit4.class)
public class FileStorageTest {

    private static final String TAG = FileStorageTest.class.getSimpleName();

    @Test
    public void test() throws Exception {
        Context context = InstrumentationRegistry.getTargetContext();
        if (context.checkSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE") == PackageManager.PERMISSION_GRANTED) {
            Log.d(TAG, "Write granted");
        } else {
            Log.e(TAG, "Write refused");
        }

        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"test.txt");
        new FileOutputStream(file).close();
    }
}
Renaud
  • 1,833
  • 19
  • 20
  • May be you also need to use `READ_EXTERNAL_STORAGE permission to read data from the device.` – Jay Rathod Jul 28 '16 at 14:09
  • @TimCastelijns Sir don't we need to define for both the permission at `Runtime` ? – Jay Rathod Jul 28 '16 at 14:12
  • @jaydroider you can but the docs say *If an app requests a dangerous permission listed in its manifest, and the app already has another dangerous permission in the same permission group, the system immediately grants the permission without any interaction with the user.* – Tim Jul 28 '16 at 14:13
  • is your sdk version >=23? – sanemars Jul 28 '16 at 14:18
  • @jaydroider yes that's it I needed to also grant READ_EXTERNAL_STORAGE. You can write the solution so I can accept it. – Renaud Jul 28 '16 at 14:42
  • @TimCastelijns No in my case WRITE was not enough, it started to work when I added READ – Renaud Jul 28 '16 at 14:43
  • 2
    Yes my mistake. Granting 1 does not grant the other, but what it does do is it automatically grants the other the moment you request it – Tim Jul 28 '16 at 14:47
  • @RenaudBlue I have added as answer below. – Jay Rathod Jul 28 '16 at 16:52
  • @TimCastelijns Sir this information is helped for more understanding :). – Jay Rathod Jul 28 '16 at 16:53

2 Answers2

1

You need to also add READ_EXTERNAL_STORAGE permission to read data from the device.

Add this permission also at RunTime.

    if (context.checkSelfPermission("android.permission.READ_EXTERNAL_STORAGE") == PackageManager.PERMISSION_GRANTED) {
            Log.d(TAG, "Read granted");
    } else {
            Log.e(TAG, "Read refused");
    }
Community
  • 1
  • 1
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
  • READ_EXTERNAL_STORAGE also exists, however, the file still cannot be read. By running in debug mode, I checked if file exists (TRUE), checked if file can be read (FALSE), checked if can be written (FALSE), checked if write and read for external storage granted (TRUE for both). Any other updated suggestion? – ranka47 Feb 25 '20 at 21:45
  • @ranka47 Please post your exact issue in new question so that can get idea what exactly issues you are facing and what you have tried please. – Jay Rathod Feb 26 '20 at 05:22
  • I had raised and seemed to be the issue with the change of how Android 10 treats its external storage. It got resolved by this question here, https://stackoverflow.com/questions/58379543/cant-create-directory-in-android-10. I resolved it by adding `android:requestLegacyExternalStorage="true"` in the manifest. – ranka47 Feb 26 '20 at 14:55
0

Try adding this into your manifest file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Chuck M.
  • 19
  • 3