我有一段代码应该检查 mp3 文件是否存储在 MediaStore.Audio.Media 内容提供商的 SD 卡中
问题是,无论情况如何。SD卡上是否存在路径名存储在变量“audioFilename”中的文件。它总是返回“此文件不存在”作为结果。尽管变量audioFilename 中存储了String 路径名“/mnt/sdCard/Music/Jungle.mp3”,而这个MP3 文件实际上是在SD 卡上。通过 Toast 消息和检查 SD 卡内容很容易证明。
我可能在使用 File 或 Environment 类时出错。任何人都可以在此处显示的代码中看到问题吗?
// 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();