0

I want to download multiple images from different URLs.

My problem is Not all photos are loaded This problem appears in emulator and mobile version 2.2

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

This run correctly on 2.3 emulator ..

.java

public class DownloadPhotos extends Activity{

Context context;

// Progress dialog --> 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<Bitmap> photos = new ArrayList<Bitmap>();

// 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<String, Bitmap, Void>
{

    // 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<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 &&
            conMgr.getActiveNetworkInfo().isAvailable() &&
            conMgr.getActiveNetworkInfo().isConnected()   );
    return connected;
}
}

Functions I used to download a photo 1.

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;
}
Leo
  • 37,640
  • 8
  • 75
  • 100
Sonnenschein
  • 169
  • 7
  • 21
  • While Egor suggested the design is not completely following the semantics, there is really no reason your approach should work sometimes and not all the time. I think you might be running into a reported bug. Can you handle the exception from decode image or also attach the log? http://stackoverflow.com/questions/1630258/android-problem-bug-with-threadsafeclientconnmanager-downloading-images. http://code.google.com/p/android/issues/detail?id=6066 – Greg Giacovelli Nov 13 '11 at 17:44

1 Answers1

0

You're completely misunderstandind the AsyncTask idea. There are three parameter types used in AsyncTask: input type, progress type and output type. In your case the input type should be String since you have an array of URI's. The progress type should be Integer if you want to show the current progress to your users, or Void if you don't. And the output type is Bitmap, cause the result of the AsyncTask is actually an array of downloaded Bitmaps. Your main problem is your AsyncTask's design. Try to correct it and run your application again, there is a high probability that it will work since then. Hope this helps.

Egor
  • 39,695
  • 10
  • 113
  • 130