gpt4 book ai didi

android - myFile.exists() 在 Android 中不符合逻辑

转载 作者:太空宇宙 更新时间:2023-11-03 12:59:31 26 4
gpt4 key购买 nike

我有一段代码应该检查 MP3 文件是否存储在 MediaStore.Audio.Media 内容提供商的 SD 卡中

问题是不管情况如何。 SD 卡上是否存在具有存储在变量“audioFilename”中的路径名的文件。结果总是返回“此文件不存在”。尽管变量 audioFilename 中存储了字符串路径名“/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();

最佳答案

你确定有 import java.io.File 吗?

尝试:

String pathsd = Environment.getExternalStorageDirectory()+"/";
// so var pathsd will return "/mnt/sdcard/"
// then you must sure var audioFilename is Music/Jungle.mp3
File myFile = new File(pathsd + audioFilename);

或者您确定自己的路径?如果你不确定你可以尝试给出直接的字符串路径。

File myFile = new File("/mnt/sdCard/Music/Jungle.mp3");

关于android - myFile.exists() 在 Android 中不符合逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13946179/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com