无法从模拟器上的外部存储读取文件
Can't read file from external storage on emulator
我正在尝试将文件从 Environment.getExternalStorageDirectory()
加载到 SoundPool
。该文件存在,但我仍然得到
E/SoundPool: error loading /storage/emulated/0/dialpad/sounds/mamacita_us/one.mp3
当我尝试加载它时。在真实设备上,它从完全相同的路径完美运行。
这是我用来加载文件的代码:
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()
是辅助方法:
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
LogCat 输出说
D/DEBUG: /storage/emulated/0/dialpad/sounds/mamacita_us/one.mp3
D/DEBUG: EXISTS
我以为可能是文件权限问题,但我不知道... adb shell
中的 ls -l
给了我
-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
曾尝试使用 chmod
更改权限,但它们从未更改过。我已经使用 adb push
将文件推送到模拟器。
有人知道如何解决这个问题吗?正如我所说,它在同一路径的真实设备上完美运行,所以这似乎是 Genymotion 模拟器 and/or 文件系统的问题。模拟器是 运行 Android 6.0 (API23).
哦,是的,我宣布
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
在我的清单中。
在 Android 6.0 中引入的新运行时权限模型下,READ_EXTERNAL_STORAGE 被视为 "dangerous permission"。
您需要请求访问外部存储的权限。
您的设备可能 运行 是 Android 版本 <6.0,因此它可以在不请求许可的情况下运行。
http://developer.android.com/guide/topics/security/permissions.html#normal-dangerous
我正在尝试将文件从 Environment.getExternalStorageDirectory()
加载到 SoundPool
。该文件存在,但我仍然得到
E/SoundPool: error loading /storage/emulated/0/dialpad/sounds/mamacita_us/one.mp3
当我尝试加载它时。在真实设备上,它从完全相同的路径完美运行。
这是我用来加载文件的代码:
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()
是辅助方法:
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
LogCat 输出说
D/DEBUG: /storage/emulated/0/dialpad/sounds/mamacita_us/one.mp3
D/DEBUG: EXISTS
我以为可能是文件权限问题,但我不知道... adb shell
中的 ls -l
给了我
-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
曾尝试使用 chmod
更改权限,但它们从未更改过。我已经使用 adb push
将文件推送到模拟器。
有人知道如何解决这个问题吗?正如我所说,它在同一路径的真实设备上完美运行,所以这似乎是 Genymotion 模拟器 and/or 文件系统的问题。模拟器是 运行 Android 6.0 (API23).
哦,是的,我宣布
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
在我的清单中。
READ_EXTERNAL_STORAGE 被视为 "dangerous permission"。
您需要请求访问外部存储的权限。
您的设备可能 运行 是 Android 版本 <6.0,因此它可以在不请求许可的情况下运行。
http://developer.android.com/guide/topics/security/permissions.html#normal-dangerous