2

I am getting the "open filed: EACCES (Permission denied)" error when trying to copy a file from assets to /sdcard from my unit test project. I spent the last two hours dredging through search and trying many variations. Probably missing some stupid little detail, hopefully someone will see it.

I am using ADT build v22.3.0-887826. Build target is Google APIs 2.2 platform. Tried 4.4.2 with same results. The AVD is 4.4.2.

The LogCat is:

MediaPlayerServiceTest(3324): copy test files
MediaPlayerServiceTest(3324): File copy testfile.mp3 exception. java.io.FileNotFoundException: /storage/sdcard/testfiles/testfile.mp3: open failed: EACCES (Permission denied)

I am testing a service that manages the media player so the class is

public class MediaPlayerServiceTest extends ServiceTestCase<MediaPlayerService> 

The code is trivial. Note the canWrite() method must be returning true.

    Log.i(TAG,"copy test files");

    File sdcard = Environment.getExternalStorageDirectory();
    if (sdcard == null) {
        Log.i(TAG,"no sdcard");
    } else {
        if (sdcard.canWrite()) {
            Log.i(TAG,"cannot write to sdcard");
        } else {
            final String fileName = "BBPro_Confirm.mp3";
            final String dest = sdcard.getAbsolutePath() + "/testfiles/" + fileName;
            if (new File(dest).exists()){
                    Log.d(TAG, "No need to copy file " + fileName);
            } else {
                    try {
                        InputStream localInputStream = mTestAppContext.getAssets().open(fileName);
                        FileOutputStream outFile = new FileOutputStream(dest);
                    } catch (IOException localIOException) {
                        Log.d(TAG, "File copy " + fileName + " exception. " + localIOException.toString());
                }
            }
        }
    }

Tried the many variations on uses-permissions that are out there. Current manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="zookey.GPS.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="zookey.GPS" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="android.test.runner" />
    </application>

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

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

I can access the sdcard from adb and created a testfiles subdirectory to verify I can update it. Irrelevant when accessing /storage/sdcard/ but also tried remounting sdcard and using /sdcard/.

root@generic:/storage/sdcard # ls -l
ls -l
drwxrwx--- root     sdcard_r          2014-02-15 13:06 Alarms
drwxrwx--x root     sdcard_r          2014-02-15 13:07 Android
drwxrwx--- root     sdcard_r          2014-02-15 13:06 DCIM
drwxrwx--- root     sdcard_r          2014-02-15 13:06 Download
drwxrwx--- root     sdcard_r          2014-02-15 13:05 LOST.DIR
drwxrwx--- root     sdcard_r          2014-02-15 13:06 Movies
drwxrwx--- root     sdcard_r          2014-02-15 13:06 Music
drwxrwx--- root     sdcard_r          2014-02-15 13:06 Notifications
drwxrwx--- root     sdcard_r          2014-02-15 13:06 Pictures
drwxrwx--- root     sdcard_r          2014-02-15 13:06 Podcasts
drwxrwx--- root     sdcard_r          2014-02-15 13:06 Ringtones
drwxrwx--- root     sdcard_r          2014-02-15 13:24 testfiles
root@generic:/storage/sdcard #

I successfully pushed a file to /storage/sdcard:

C:\Users\Bruce\android-sdks\platform-tools>adb -s emulator-5554 push F:\BBPro_link.mp3 /storage/sdcard/testfiles
100 KB/s (5464 bytes in 0.053s)

Rebuilt the ADV several times. Etc, etc, etc

Any suggestions would be very helpful.

sarge
  • 21
  • 4
  • There is a bug in the code. "if (sdcard.canWrite())" should be "if (!sdcard.canWrite())". The LogCat now says "cannot write to sdcard". Still does not answer the question why it cannot write to the sdcard but at least the canWrite() method is behaving correctly. – sarge Feb 15 '14 at 20:45
  • Might have something to do with being a test project. Here is another person who was dealing with the same issue. No solution for him either. http://stackoverflow.com/questions/11459401/android-testing-the-storage-state-is-mounted-the-write-external-storage-permis – sarge Feb 15 '14 at 23:09
  • Yes, it has something to do with being a test project. I created a simple service in my main app to copy the files and it works just fine. This seems like a reasonable request, being able to copy test files (in this case audio files) from the unit test project to the sdcard so the main app can access them while running unit tests. – sarge Feb 15 '14 at 23:44

2 Answers2

0

Insert in your manifest file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
a.black13
  • 2,157
  • 4
  • 19
  • 20
0

The solution was to create a small service in my main app to copy the files. Called this service from the unit test during setup. Used an if exists to prevent copying the files with each test setup.

sarge
  • 21
  • 4