Issue
I am trying to solve this problem for hours now and can't find a proper solution: I am trying to Play a media File (Mp3/Ogg) but always get the FileNotFoundException (and I'm sure it's there ;) )
This is what I try:
Check if SD Card is available.
Check if reading/writing permissions are granted.
Load/Play Song.
if(isExternalStorageReadable() && isExternalStorageWritable())
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Log.v("Permission","Readingpermission is granted");
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Log.v("Permission","Writingpermission is granted");
}
playSong(getExternalFilesDir(Environment.DIRECTORY_MUSIC).toString() + "/t1.mp3");
That part of the code is working so far.
But as soon as it comes to the playSong(String) :
try {
MediaPlayer mediaplayer = new MediaPlayer();
mediaplayer.setDataSource(path);
mediaplayer.prepare();
mediaplayer.start();
} catch (Exception e){
Log.e("Mediaplayer", e.toString());
}
The program crashes trying to setDataSource with this Exception:
E/Mediaplayer: java.io.FileNotFoundException: /storage/emulated/0/Android/data/OMSclient.omsgui/files/Music/t1.mp3
What am I missing? I'm clueless.
Alright, I've found the following problem:
If I simply execute
playSong("storage/17E5-1C14/Android/data/OMSclient.omsgui/files/Music/t1.ogg");
It works fine, but if I Use:
playSong("/storage/emulated/0/Android/data/OMSclient.omsgui/files/Music/t1.mp3");
or
playSong("storage/emulated/0/Android/data/OMSclient.omsgui/files/Music/t1.mp3");
It does not.
Why? ...
Solution
Alright. Because getExternalFilesDir did not seem to work, I used the following workaround:
public String getExternalPath(){
String removableStoragePath = "";
File fileList[] = new File("/storage/").listFiles();
for (File file : fileList)
{ if(!file.getAbsolutePath().equalsIgnoreCase(Environment.getExternalStorageDirectory().getAbsolutePath()) && file.isDirectory() && file.canRead())
removableStoragePath = file.getAbsolutePath(); }
return removableStoragePath;
}
This code worked fine on every Emulator I tested it. Hope this helps, if somebody had the same problem.
Answered By - Developer Ol
Answer Checked By - Katrina (JavaFixing Volunteer)