How to check whether a file exists in (Internal/External storage) by using file name?

时间:2017-12-18 08:29:19

标签: java android file

I have been working on a project where I will get a filename from API and have to check whether the file is present in the device and play it.

I am getting REPLICATE as filename from API and under the same name a file is present in my storage.

But when I used the piece of code, I have been returned false. I have checked with other codes also.

Screenshot.jpg

where am I going wrong? Any help would be greatly appreciated!

3 个答案:

答案 0 :(得分:1)

Probably, what you do wrong is using gradle -h -g, --gradle-user-home Specifies the gradle user home directory .

Instead, use this.getFilesDir() for example, it's all dependant on where your file is.

Like I said before, debug it yourself, print (or present a toast) with the 'expected' file path, then verify it doesn't exist

答案 1 :(得分:0)

Try something like that :

setInterval(() => {
  if (dankMiner > 0) {
    borkCoins += dankMiner;
    displayScreen();
  }
}, 1000);

答案 2 :(得分:0)

使用Environment.getExternalStorageDirectory()这就是获取文件目录的方式

然后你会在它之后添加你的文件路径,所以你应该做这样的事情

if(!new File(Environment.getExternalStorageDirectory().toString()+"/myFolder/"+"myFile").exist())
{
// file is not exist
}

请记得检查运行时权限,因为它是特殊权限