0

Below code started failing with Error 09-07 15:42:52.174: W/System.err(11739): java.io.FileNotFoundException: /storage/sdcard/NumberType.txt: open failed: EACCES (Permission denied)

Strangely I am able to read the files as expected. It only fails as soon as I try to write something. It's failing in line fileOut = new FileOutputStream(file,true); with below trace.

09-07 15:42:52.174: W/System.err(11739): java.io.FileNotFoundException: /storage/sdcard/DixitMobileUsage/NumberType.txt: open failed: EACCES (Permission denied)
09-07 15:42:52.177: W/System.err(11739):    at libcore.io.IoBridge.open(IoBridge.java:456)
09-07 15:42:52.177: W/System.err(11739):    at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
09-07 15:42:52.177: W/System.err(11739):    at ind.cosmos.utilities.FileManager.appendContentToBottomInFile(FileManager.java:263)
09-07 15:42:52.177: W/System.err(11739):    at ind.cosmos.main.UNKNOWN_POPUP$1$1.onClick(UNKNOWN_POPUP.java:117)

Code :

FileOutputStream fileOut = null;
BufferedWriter bw = null;
try 
{
    fileOut = new FileOutputStream(file,true);
    bw = new BufferedWriter(new OutputStreamWriter(fileOut, "8859_1"));
    if(null != comment)
        bw.write(key+"="+value+" \t\t# "+comment);
    .
    .

I have also mentioned the permissions in the manifest.xml

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

I have also searched a lot over net. No solution works for me. It's not even API 23 where I have to write the permission code. :(

Please help.

Bharat
  • 750
  • 1
  • 9
  • 20
  • like I need to handle in the code as well ? But I saw .. it needed only after API 23.. Isn't ? – Bharat Sep 07 '16 at 10:42
  • Runtime permissions have to be requested at API 23 and above, no need here unless you test your app on an device with API 23 and above, also if the target is API21. For example, your target is API21, but you test it on a marshmallow device, it could be, that the permission is not requested as default. In that case, you have to inform the user about and he has to do it manually. The next point is, if you got a custom rom like cyanogenMod, you have to mount all your relevant storages (seen on Samsung Galaxy S3 with CM13)... – Opiatefuchs Sep 07 '16 at 10:46
  • Check out this link: http://stackoverflow.com/questions/21200428/android-exception-java-io-ioexception-open-failed-eacces-permission-denied – Abdullah Mehmood Sep 07 '16 at 10:59
  • ...but be aware in case of CM: I have detected, that even if I mount my storage, the first one or two tries, the app throws the same exception and after that ut worked like magic.... – Opiatefuchs Sep 07 '16 at 11:15
  • Where is the value for `file` coming from? The value output in LogCat does not look correct. – CommonsWare Sep 07 '16 at 11:19

0 Answers0