Download multiple photos within a loop in android?
<p>I want to download multiple images from different URLs.</p>

<p>My problem is <strong>Not all photos are loaded</strong>
This problem appears in emulator and mobile version 2.2</p>

<p>If I want to download 6 photos there only 5 while the app. running.
If I want to download 25 photos there only 12 ,16 or 20  while the app. running.
each time I run the emulator there is a different result :S</p>

<p>This run correctly on 2.3 emulator .. </p>

<p>.java</p>

<pre><code>public class DownloadPhotos extends Activity{

Context context;

// Progress dialog --&gt; shows that something is loading 
ProgressDialog dialog;

// the layout where we insert the loaded pictures
LinearLayout linlayout;

// where we put all bitmaps after download
ArrayList&lt;Bitmap&gt; photos = new ArrayList&lt;Bitmap&gt;();

// URLs of photos we want to download
String [] urls = {
        "http://www.flowersegypt.net/upload/Flowers-Egypt-6.jpg","http://www.kabloom.com/images/product_images/KB_11100.jpg"
        ,"http://faisal-saud.com/wp/wp-conteant/uploads/2010/09/QuilledFlowers.jpg",
        "http://i3.makcdn.com/wp-content/blogs.dir/144387/files//2009/11/wedding-flowers1.jpg",
        "http://www.funonthenet.in/images/stories/forwards/flowers/Blue-Bell-Tunicate.jpg",
        "http://flowersfast.com/f4322dl.jpg"
}; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    // Setting the layout of the page to "downloadphoto.xml" layout
    setContentView(R.layout.downloadphoto);

    // Bind the previously defined layout with the lyout in the xml
    linlayout = (LinearLayout) findViewById(R.id.linearLayout1);

    this.context=this;

    //Checking if Internet is connected
    if(CheckConnection())
    {
        // Starts in Threads
        new THREAD1().execute("");
    }
}

//========== Threads ============
//===============================
private class THREAD1 extends AsyncTask&lt;String, Bitmap, Void&gt;
{

    // This function shows the progress dialog 
    // and it works on foreground 
    // while the needed data is loaded

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        dialog = ProgressDialog.show(context , "", 
                "Loading. Please wait...", true);
    }   


    // This function is responsible for loading the data 
    // and it works in the background

    @Override
    protected Void doInBackground(String... params) {
        // TODO Auto-generated method stub

        //get all bitmaps of the predefined URLs
        for(int i=0 ;i&lt;urls.length;i++)
        {               
            Bitmap tmp = Networking.getimage(urls[i]);
            if(tmp != null)
            {
                photos.add(tmp);

                // This line calls the function "onProgressUpdate" for each loaded bitmap
                publishProgress(tmp);       
            }
        }
        return null;
    }

    //this function is called in ech time 
    @Override
    protected void onProgressUpdate(Bitmap... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);     
        ImageView image = new ImageView(context);
        image.setImageBitmap(values[0]);//ba3red el bitmap
        image.setScaleType(ScaleType.CENTER);
        image.setClickable(true);
        image.setPadding(10,10, 10,10);
        linlayout.addView(image);
        dialog.cancel();
    }
    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
    }
}

// This function returns true if Internet connected otherwise returns false
public Boolean CheckConnection()
{
    ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 
    boolean connected=(   conMgr.getActiveNetworkInfo() != null &amp;&amp;
            conMgr.getActiveNetworkInfo().isAvailable() &amp;&amp;
            conMgr.getActiveNetworkInfo().isConnected()   );
    return connected;
}
}
</code></pre>

<p>Functions I used to download a photo
1.</p>

<pre><code>public static Bitmap getimage(String URL)
{
    Bitmap bitmap=null;
    InputStream in=null;
    try {
        // all spaces must be replaced by 20%
        String tmp = URL.replaceAll(" ", "20%");
        in = OpenHttpConnection(tmp);
        BitmapFactory.Options options=new BitmapFactory.Options();
        // deh mas2ola enha trag3 1/2 el image
        options.inSampleSize = 2 ;
        options.inScaled = true;
        bitmap = BitmapFactory.decodeStream(in, null, options);
        in.close();
    } catch (Exception e) {
        // TODO: handle exception
    }
    return bitmap;
}
</code></pre>
