Search code examples
androidandroid-emulatoradbgenymotionandroid-file

Can't read file from external storage on emulator


I'm trying to load a file from Environment.getExternalStorageDirectory() into a SoundPool. The file exists, but still I get

E/SoundPool: error loading /storage/emulated/0/dialpad/sounds/mamacita_us/one.mp3

when I try to load it. On a real device it works perfectly, from the exact same path.

This is the code I'm using to load the file:

if( isExternalStorageReadable() ){
    String externalStorageAbsolutePath = Environment.getExternalStorageDirectory().getAbsolutePath();
    String soundFilesPath = (new File(externalStorageAbsolutePath, "dialpad/sounds/mamacita_us")).getPath();

    Log.d("DEBUG", (new File(soundFilesPath, "one.mp3")).getAbsolutePath() );
    Log.d("DEBUG", (new File(soundFilesPath, "one.mp3")).exists() ? "EXISTS" : "DOES NOT EXSIST");

    sounds.put("1", soundPool.load((new File(soundFilesPath, "one.mp3")).getAbsolutePath(), 1));
    sounds.put("2", soundPool.load( (new File(soundFilesPath, "two.mp3")).getPath(), 1 ));
}

isExternalStorageReadable() is a helper method:

public boolean isExternalStorageReadable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state) ||
            Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
    }
    return false;
}

The LogCat outputs say

D/DEBUG: /storage/emulated/0/dialpad/sounds/mamacita_us/one.mp3
D/DEBUG: EXISTS

I had an idea that perhaps it was a file permission issue, but I don't know... ls -l in adb shell gives me

-rw-rw---- root     sdcard_rw    18288 2009-07-06 13:42 one.mp3
-rw-rw---- root     sdcard_rw    21422 2009-07-06 13:44 two.mp3

Have tried changing the permissions using chmod but they never change. I've used adb push to push the files to the emulator.

Anyone have any idea how to solve this? As I said it's working perfectly on a real device for the same path, so it seems to be an issue with the Genymotion emulator and/or file system. The emulator is running Android 6.0 (API23).

Oh, and yes I'm declaring

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

in my manifest.


Solution

  • READ_EXTERNAL_STORAGE is considered a"dangerous permission" under the new runtime permissions model introduced in Android 6.0.

    You will need to request permission to access the external storage.

    Your device is probably running an Android version <6.0 so it will work on that without requesting permission.

    http://developer.android.com/guide/topics/security/permissions.html#normal-dangerous