4

I am suffering from an "open failed: EACCES (Permission denied)" on Android when I attempt to write to "/mnt/sdcard/report/". My 1st step is to create the "report" folder which doesn't work. I then attempt to write which throws the above exception. I have set <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> as a peer (outside of) the application tag. I am trying to run a JUnit instrumentation test on the emulator when I get the error. Any ideas? Code is below.

        if (mReportDir == null) {
            if (mContext.getFilesDir() != null) {
                mOutputStream = mContext.openFileOutput(fileName, 0);
            } else {
                mOutputStream = mTargetContext.openFileOutput(fileName, 0);
            }
        } else {
            File f = new File(mReportDir);
            if (!f.exists()) {
                f.mkdirs();
            }
            mOutputStream = new FileOutputStream(new File(mReportDir, fileName));
        }

mReportDir is equal to "/mnt/sdcard/report", fileName is equal to "junit-report.xml", f.mkdirs returns false I believe and the dir is never created. I'm wondering why I get permission denied. I'm trying to reuse a custom JUnit Test runner.

I've added hw.sdCard= yes to the avd settings. After launching I shell in and type mount:

~$ adb shell
# mount
rootfs / rootfs ro 0 0
tmpfs /dev tmpfs rw,nosuid,mode=755 0 0
devpts /dev/pts devpts rw,mode=600 0 0
proc /proc proc rw 0 0
sysfs /sys sysfs rw 0 0
none /acct cgroup rw,cpuacct 0 0
tmpfs /mnt/asec tmpfs rw,mode=755,gid=1000 0 0
tmpfs /mnt/obb tmpfs rw,mode=755,gid=1000 0 0
none /dev/cpuctl cgroup rw,cpu 0 0
/dev/block/mtdblock0 /system yaffs2 ro 0 0
/dev/block/mtdblock1 /data yaffs2 rw,nosuid,nodev 0 0
/dev/block/mtdblock2 /cache yaffs2 rw,nosuid,nodev 0 0

Again, I am launching the AVD programmatically from an Ant script. Is there something I can do programmatically to mount the sdcard?

Cliff
  • 10,586
  • 7
  • 61
  • 102
  • Have you seen http://stackoverflow.com/questions/2121833/permission-to-write-to-the-sd-card -- specifically the WRITE_EXTERNAL_STORAGE permission? – mah Sep 13 '12 at 21:43
  • Yes, my apologies! SO ate my uses-permissions tag in my post. I've edited the post please re-read. :) – Cliff Sep 13 '12 at 21:45
  • Can you show the code you're using to try to create the directory (as you're implying it doesn't already exist and that's your current point of failure)? – mah Sep 13 '12 at 21:48
  • Done. Also, I'm currently looking to see if I need to explicitly mount the sd card when I launch the emulator. I currently launch it from the command line. – Cliff Sep 13 '12 at 22:00
  • 2
    Is `/mnt/sdcard` = `Environment.getExternalStorageDirectory()`? If not you might be a victim of http://www.chainfire.eu/articles/113/Is_Google_blocking_apps_writing_to_SD_cards_/ – zapl Sep 13 '12 at 22:12
  • 1
    On a glance, your code looks fine -- I was primarily looking to ensure you were calling mkdir() [or mkdirs(), as you are]. As to mounting the virtual card in the emulator -- that may be the issue. If you `adb shell` you can type the command `mount` to see what is mounted. On my emulator (no virtual sd card), `ls -l /mnt` shows that /mnt/sdcard has no access permissions at all -- `d--------- system system 2012-09-12 10:55 sdcard` – mah Sep 13 '12 at 22:12

1 Answers1

10

Found the problem! When I created the AVD I never specified the size of the SD card. I edited and set a size of 512MiB and now it's writeable! Thanks all who replied! :)

Cliff
  • 10,586
  • 7
  • 61
  • 102