2013-02-23 75 views
1
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(); 
} 

我正在使用上面的代码来检查文件是否存在。我用一个文件检查了它,它工作正常,但是当我在有多个(100-200)图像的应用程序中使用它时,它总是开始下载文件,无论它们是否存在。有没有比这更好的方法?如何检查文件的存在?

+0

我认为它应该单词。你确定路径是正确的吗? – 2013-02-23 10:18:17

+1

无法从您单独发布的代码中分辨出来。为什么你将路径存储在'imagepath'列表中,如果你从图像ID构造它?也许你正在检查错误的路径(btw。硬编码sd卡的路径是不好的做法,使用'Environment.getExternalStorageDirectory()'作为root或'Context.getExternalCacheDir()'来确保它在所有版本) – Thrakbad 2013-02-23 10:21:53

回答

1

首先检查文件夹上的许多图像如何: -

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

以上后运行循环,并检查ü[R条件: -

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

硬编码路径不是一个好主意。 – Reno 2013-02-23 10:29:14

+0

我只是给他的想法,但我右@Reno – duggu 2013-02-23 10:34:56