0

In my project I have a code for downloading images from a server. In php code I have:

if (is_dir($dir)){ //check if is directory
    $files = scandir($dir); //list files
    $i=-1;
    $result = array();

    foreach($files as $file){ 
        $file = $dir.$file; //complete dir of file
        if(is_file($file)){ //if it's a file
            $i++;
            $fileo = @fopen($file,"rb"); //open and write it on $result[$i]
            if ($fileo) {
                $result[$i] = "";
                while(!feof($fileo)) {
                    $result[$i] .= fread($fileo, 1024*8);
                    flush();
                    if (connection_status()!=0) {
                        @fclose($fileo);
                        die();
                    }
                }
                $result[$i] = utf8_encode($result[$i]); //This prevents null returning on jsonencode
                @fclose($fileo);
            }   
        }
    }
}else{
    echo json_encode(array('Error'));
}
echo json_encode($result); //returns images as an array of strings, each one with all code of an image.

And in java I have an httpost method with asynctask. The problem of this is that I need to wait until all images are downloaded. I have thought that maybe I can download an image with a number and if the number is 0 for example recall to the server to download the next image but I think that maybe is a waste of time calling again the server and listing files again. Is there a better way so I can download image per image instead of all at the same time without calling N times to the sever?

Learning from masters
  • 2,032
  • 3
  • 29
  • 42
  • Maybe this help you: [http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog?rq=1][1] [1]: http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog?rq=1 – Daniel Vaquero Jul 30 '13 at 11:25
  • return `["imagefilename1", "imagefilname2", ... ,"imagefilname3"]` from php then ask server for each image with some LazyImageLoader ... – Selvin Jul 30 '13 at 11:26

1 Answers1

0

I've had the same problem that you are having, and my solution was the next:

1º Download all data you need 2º Download to the computer the image web direction 3º Execute an asynctask per Image to download it and update your Activity while is needed

Maybe i'm not so exact and i would need some more details to give a better solution.

n4h1n
  • 357
  • 5
  • 19