0

I'm getting an exception that file open permission is denied when I'm trying to open a FileInputStream.

 File myFile = new File(Environment.getExternalStorageDirectory()
           .getAbsolutePath() + "/test/test.txt");
 try {
      FileInputStream inStream = new FileInputStream(myFile); // crash

Here's the stacktrace:

 System.err﹕ java.io.FileNotFoundException: /storage/emulated/0/test/test.txt: open failed: EACCES (Permission denied)
 System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:456)
 System.err﹕ at java.io.FileInputStream.<init>(FileInputStream.java:76)

 ...

 Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
 at libcore.io.Posix.open(Native Method)
 at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
 at libcore.io.IoBridge.open(IoBridge.java:442)

And yes, I do have the permissions in my manifest:

<uses-permission android:name="ANDROID.PERMISSION.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE" />
David M
  • 841
  • 6
  • 23
  • possible duplicate of [open failed: EACCES (Permission denied)](http://stackoverflow.com/questions/23527767/open-failed-eacces-permission-denied) – BSMP Jun 10 '15 at 19:41

1 Answers1

3

Android is case sensitive. Replace ANDROID.PERMISSION with android.permission:

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

Also, you should only need one of those permissions. If you are planning on writing to external storage, you should not also need READ_EXTERNAL_STORAGE.

CommonsWare
  • 954,112
  • 185
  • 2,315
  • 2,367
  • Right, thanks. AndroidStudio auto-completed it in all caps. Totally missed that. – David M Jun 10 '15 at 19:52
  • @DavidM: "AndroidStudio auto-completed it in all caps" -- seriously? What version of Android Studio are you running? I'd like to see if I can reproduce it and, if I can, get a bug report filed for it. Thanks! – CommonsWare Jun 10 '15 at 19:54
  • Yeah I typed – David M Jun 10 '15 at 19:56
  • @DavidM: That issue is already reported, as it turns out: https://code.google.com/p/android/issues/detail?id=173977 – CommonsWare Jun 10 '15 at 21:31
  • have the same problem, but this doesn't solve it :/ does anyone know what else can be the reason of exception? – Carmine Jan 17 '16 at 10:02