I have a section of code that is supposed to check if an mp3 file is stored in the SD card on the MediaStore.Audio.Media content provider
the problem is that no matter the situation. Either if the file with the pathname stored in variable “audioFilename” exists on the SD card or not. it always returns “this file does not exist” as the result. Despite the fact that the variable audioFilename has the String path name stored in it “/mnt/sdCard/Music/Jungle.mp3”, and this MP3 file is actually on the SD card. Easy to prove with a Toast message and a check of the SD card contents.
I probably have an error in the use of File or Environment classes. Can anyone see a problem in the code shown here?
// toast message to prove that the audioFilename is not null,
// message displayed is the string, "File name: /mnt/sdCard/Music/Jungle.mp3"
Toast.makeText(Editor.this, "File name: " + audioFilename,
Toast.LENGTH_LONG).show();
// the code below always returns "this file does not exist"
File extStore = Environment.getExternalStorageDirectory();
File myFile = new File(extStore.getAbsolutePath() + "audioFilename");
if(myFile.exists()){
Toast.makeText(Editor.this, "<<<< this file exists, it is: "+audioFilename+" >>>>",
Toast.LENGTH_LONG).show();
} else if(!myFile.exists()){
Toast.makeText(Editor.this, "<<<< this file does not exist >>>> ",
Toast.LENGTH_LONG).show();
}
Toast.makeText(Editor.this, "audio file name is: "+ audioFilename,
Toast.LENGTH_LONG).show();
Try to change this line
with
I have run test with the same code and it is working well.
Here is the code I test with :
Hope it helps you.
Thanks.