Question

i am creating an application for android in eclipse. i have a chat application in which user's contact's images are downloaded in sdcard and are put in a HashMap (url, localAddress), when i want to load contact list, for any contacts I use a function to find address of contacts' pictures on sdcard, three different state may occur 1- image found on sdcard then return path. 2- image don't download before and HashMap return null, then download it. 3- HashMap return a path but user deleted image from sdcard then remove key from Hashmap and download again.

my code:

public static String findFile(Context ctx, String url, String type)
{
    try
    {
        String value = globalVars.addresses.get(url);
        if(value != null)
        {
            File ff = new File(value);

            if(ff.exists())
                return value;

            globalVars.addresses.remove(url);
        }
        globalVars.enqueueJob(ctx, new globalVars.downloadJob(url, url, type));
        return null;
    }
    catch(Exception ex)
    {
        Log.e("Find File", "Start");
        Log.e("Find File", ex.toString());
        ex.printStackTrace();
        return null;
    }
}

but when i delete an image it download more and more .

function enqueueJob :

    public static void enqueueJob(Context context, downloadJob dj)
{
    if(inQueue.get(dj.getAddress())!= null && inQueue.get(dj.getAddress())== true)
        return;
    downloads.add(dj);
    inQueue.put(dj.address, true);
    FileUtils.doDownload(context);
}

It work's fine for pictures don't download yet and pictures that download and don't delete yet.

Was it helpful?

Solution

Your title is confusing as it indicates you suspect the File.exists as the problem. Looking at your code, the fact that deleted images are added to download queue means File.exists works fine at least the first time. Otherwise, you would not even add it to the queue. Now the question is what happens when it is added to queue and downloaded. Check your code to make sure that it added back to the map properly. In your code, something should be added to the download queue only under two conditions: one if nothing in the map and the other is whatever in the map is deleted. So either it is not added back properly or may be a race condition where your File.exists is happening before the download is completed fully and you are adding it back to the queue again.

Good luck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top