简体   繁体   English

如何检查文件的存在?

[英]How to check for the existence of a file?

String extra=imagepath.get(i).toString();
String extra2=imageid.get(i).toString();
String path = "/mnt/sdcard/Android/data/com.example.webdata/files/Download/" + extra2 + ".jpg";
File f = new File(path);
if(f.exists())
{ 
    //Toast.makeText(getApplicationContext(), "File already exists....",
    //  Toast.LENGTH_SHORT).show();

}
else
{
    downloadfile();
}

I am using the above code to check if a file exists or not. 我正在使用上面的代码来检查文件是否存在。 I have checked it with one file and it works fine, but when I use it in my application where there are multiple (100-200) images it always starts downloading files whether they exist or not. 我已经检查了一个文件,它可以正常工作,但是当我在有多个(100-200)图像的应用程序中使用它时,无论是否存在,它总是开始下载文件。 Is there a better method than this? 有没有比这更好的方法了?

First check how many images on folder:- 首先检查文件夹中有多少张图片:-

File extStore = Environment.getExternalStorageDirectory();
File[] imageDirs = extStore.listFiles(filterForImageFolders);

after above you run the loop and check ur condition:- 在上面之后,您运行循环并检查您的状况:-

for(int i=0;i<list.length;i++)
{
     // ur condition
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM