简体   繁体   中英

android eclipse emulator sdcard permission denied

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.

插入清单文件中:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM