0

I have an activity which creates a new java object and it executes methods inside that object. One of the methods uses a fileoutputstream with a file and directory defined in the activity and the method includes writing some data to this file and then closing it. I've tested this on version 4 and 5 devices and it works with no problems but when I go to an android 6/7 device, the fileoutputstream throws a FileNotFoundException claiming that the there is no such file or directory and the app crashes but I'm confused because it does exist (file name/directory doesn't change between the different versions) as if it didn't exist, the earlier versions of android would have thrown this error as well.

I understand there was a change in how permissions were handled for users from android 5 to android 6 but I don't know how or whether to use that to fix the issue. The permission to write to external storage is already in my Manifest file , the file directory includes the Environment.getExternalStorageDirectory() method and the current target sdk is 23 (android version 6).

Please advise on what might be causing the problem and what I would need to do to resolve this.

bardsleyta
  • 151
  • 1
  • 2
  • 14
  • Please check the below link. https://stackoverflow.com/questions/33162152/storage-permission-error-in-marshmallow – Adarsh Nair Aug 03 '17 at 01:13

1 Answers1

0

I managed to resolve this issue by checking what the sdk version of the device was and if it was version 23 (android 6) or higher, then I check if self permission is given for the appropriate permissions and if it isn't, I then call ActivityCompat.requestPermissions and this fixes the problem.

bardsleyta
  • 151
  • 1
  • 2
  • 14