简体   繁体   中英

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. 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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