1

Am unable to write to external storage in android. I do get the following stacktrace: 01-14 10:57:07.108: W/System.err(17380): java.io.FileNotFoundException:

/mnt/media_rw/extSdCard/SterlingPixels.apk: open failed: EACCES (Permission denied)
01-14 10:57:07.108: W/System.err(17380):    at libcore.io.IoBridge.open(IoBridge.java:456)
01-14 10:57:07.108: W/System.err(17380):    at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
01-14 10:57:07.108: W/System.err(17380):    at java.io.FileOutputStream.<init>(FileOutputStream.java:127)
01-14 10:57:07.108: W/System.err(17380):    at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
01-14 10:57:07.108: W/System.err(17380):    at com.codename1.impl.android.AndroidImplementation.createFileOuputStream(AndroidImplementation.java:4258)
01-14 10:57:07.108: W/System.err(17380):    at com.codename1.impl.android.AndroidImplementation.openFileOutputStream(AndroidImplementation.java:4210)
01-14 10:57:07.108: W/System.err(17380):    at com.codename1.io.FileSystemStorage.openOutputStream(FileSystemStorage.java:263)
01-14 10:57:07.108: W/System.err(17380):    at com.codename1.io.ConnectionRequest.readResponse(ConnectionRequest.java:783)
01-14 10:57:07.108: W/System.err(17380):    at com.codename1.io.ConnectionRequest.performOperation(ConnectionRequest.java:440)
01-14 10:57:07.108: W/System.err(17380):    at com.codename1.io.NetworkManager$NetworkThread.run(NetworkManager.java:263)
01-14 10:57:07.108: W/System.err(17380):    at com.codename1.impl.CodenameOneThread$1.run(CodenameOneThread.java:60)
01-14 10:57:07.108: W/System.err(17380):    at java.lang.Thread.run(Thread.java:818)
01-14 10:57:07.108: W/System.err(17380): Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
01-14 10:57:07.108: W/System.err(17380):    at libcore.io.Posix.open(Native Method)
01-14 10:57:07.108: W/System.err(17380):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
01-14 10:57:07.108: W/System.err(17380):    at libcore.io.IoBridge.open(IoBridge.java:442)
01-14 10:57:07.108: W/System.err(17380):    ... 11 more

I cant include "<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>" in build hints, as this causes the build to fail on merging the manifests..so i assume the build system creates this permission correctly. Except it does not work. Or am i mistaken?

davidwaf
  • 299
  • 2
  • 9
  • 2
    Is it possible your device is connected as a USB - http://stackoverflow.com/questions/8854359/exception-open-failed-eacces-permission-denied-on-android – Chen Jan 14 '16 at 12:12

2 Answers2

1

I had usb connected indeed, but that did not resolve my problems right away after disconnecting it. So,at first i was getting the path to write the file as follows:

 String root = roots[0];
        for (String root1 : roots) {
            if (FileSystemStorage.getInstance().getRootType(root1) == FileSystemStorage.ROOT_TYPE_MAINSTORAGE) {
                root = root1;
                break;
            }
        }
        root= root + FileSystemStorage.getInstance().getFileSystemSeparator();

For some strange reason, the above code was always returning external storage; i imagine it is supposed to return internal phone storage. So this always gave me the permissions headache. This is on Android 5.1.1.

I did change this to

String root = FileSystemStorage.getInstance().getAppHomePath();

which of course worked, but this certainly does not address the permission problem in my case.

davidwaf
  • 299
  • 2
  • 9
0

You don't need to add the WRITE_EXTERNAL_STORAGE permission in Codename One as it is requested by default. Newer builds migrated to gradle which is more strict than Ant on manifest usage so you are getting the error.

Make sure your device isn't connected as a mass storage device which sometimes causes a conflict in file access on sd card.

Shai Almog
  • 51,749
  • 5
  • 35
  • 65