Reputation: 571
Im trying to curb duplicate downloads by checking if the file a user is about to download already exists before I proceed to downloading it. Im using the method File.exists() to check for this condition but it seems to be always returning true. Below is my code:
File destination = new File(Environment.DIRECTORY_DOWNLOADS, "/" + playlistTitle + "/" + image.getImgTitle() + ".jpg");
if (!destination.exists()) {
downloadId = downloadImage(image.getImgURL(), image.getImgTitle(), playlistTitle);
downloadIds.add(downloadId);
}
If I go directly to that file path before its created, it seems to be empty, but the code above returns true regardless.
Upvotes: 3
Views: 1510
Reputation: 11224
new File(Environment.DIRECTORY_DOWNLOADS, "/" + playlistTitle
That can only lead to a path that is not possible.
You should use instead
new File( Environment.getExternalPublicDirectory(
Environment.DIRECTORY_DOWNLOADS),......
Upvotes: 0
Reputation: 11018
You can check few things here
first as expected we are getting the permission to read and write the storage media.
and you try also using below method for checking if a file can be read or not
if (!file.canRead()) {
return false;
}
if two above doesn't work then use this.
if (!file.isFile()) {
//file doesn't exist
}else{
//file exists
}
Upvotes: 1