1

I am currently trying to make an app create and write into a file in external storage. However, I get the java.io.FileNotFoundException: /storage/emulated/0/input.csv: open failed: EACCES (Permission denied) error when trying to open the file.

I have looked around StackOverflow, and the problem is happening because I have the phone plugged into the computer for debugging purposes as the directory path is correct and I have <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in the AndroidManifest.xml (between <manifest> and <application>).

My question is, is there a way to fix this error while still having USB debugging active? I need to maintain the ADB connection and monitor the logcat, for now, to see if all parts of the app are working as expected.

For completeness' sake, here is the code I use to attempt to create a file.

File input = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/input.csv");
FileWriter writer = new FileWriter(input);
writer.append(csvString);
writer.flush();
writer.close();
Obito
  • 391
  • 3
  • 8
Schlez
  • 91
  • 9
  • Is `input.csv` created by your application? – Madhukar Hebbar Oct 05 '16 at 13:30
  • Yes, `input.csv` should be created inside the external storage directory. – Schlez Oct 05 '16 at 13:31
  • What's the android version? if it's greater than marshmallow then you should enable `Storage` permission for your application. – Madhukar Hebbar Oct 05 '16 at 13:33
  • He means runtime permission programmatically. Or manually using the settings for your app. – greenapps Oct 05 '16 at 13:34
  • The device itself is running Cyangenmod 12 but the "Compile Sdk Version" is API 23: Android 6.0 (Marshmellow). Do you mean ``? I have enabled that already in the manifest. – Schlez Oct 05 '16 at 13:36
  • check `Environment.getExternalStorageState()` before writing (and print it to the log for debugging) – Alex P. Oct 05 '16 at 13:49
  • It just says `mounted`. I actually found the solution. If you set the usb connection to camera the error does not come up. – Schlez Oct 05 '16 at 16:42
  • it says `mounted` now. but before you changed the usb config it used to say something else - which would have helped to determine the cause of the probem – Alex P. Oct 05 '16 at 18:04
  • It actually just said `mounted` before I found the usb config workaround. – Schlez Oct 05 '16 at 18:35
  • @Schlez you should get **Runtime** permission for `storage`, [ex1](http://www.truiton.com/2016/04/obtaining-runtime-permissions-android-marshmallow-6-0/) and [ex2](http://stackoverflow.com/questions/34342816/android-6-0-multiple-permissions/35495855#35495855) link for examples – Madhukar Hebbar Oct 06 '16 at 05:33

0 Answers0