-1

Hello how can i write this code in a separate threads because i have an intensive operations in my main thread.so i have to use an Async Activity and delegate the network intensive operation to 'doInBackground' method.but i don't know how to edit it

   public void setImage(ImageView aView, final URL aURL) throws IOException {
        final Bitmap bm = null;
        AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                URLConnection conn = null;
                try {
                    conn = aURL.openConnection();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    conn.connect();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                InputStream is = null;
                try {
                    is = conn.getInputStream();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                // Bufferisation pour le t�l�chargement
                BufferedInputStream bis = new BufferedInputStream(is, 8192);

                // Cr�ation de l'image depuis le flux des donn�es entrant
                bm = BitmapFactory.decodeStream(bis);
                try {
                    bis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return null;
            }
        };
        task.execute(null, null, null);
        // Fixe l'image sur le composant ImageView
        aView.setImageBitmap(bm);
    }

please help me thanks

Bilel
  • 25
  • 11
  • 1
    Have you tried moving this into an AsyncTask yet? Have you read the Using an AsyncTask section of the [Processes and Threads documentation](http://developer.android.com/guide/components/processes-and-threads.html#WorkerThreads)? Once you have, post the AsyncTask you have created and we can help with fixing it. – Bryan Herbst Sep 27 '13 at 20:55

2 Answers2

0

Try This

public void setImage(ImageView aView, URL aURL) {
    try {
        Bitmap bm;
        AsyncTask<URL, Void, Bitmap> task = new AsyncTask<URL, Void, Bitmap>() {

            @Override
            protected Bitmap doInBackground(URL... params) {
                URLConnection conn = params[0].openConnection();
                conn.connect();
                InputStream is = conn.getInputStream();

                // Bufferisation pour le t�l�chargement
                BufferedInputStream bis = new BufferedInputStream(is, 8192);

                // Cr�ation de l'image depuis le flux des donn�es entrant
                Bitmap bm = BitmapFactory.decodeStream(bis);
                bis.close();
                is.close();
                return bm;
            }
        };
        bm = task.execute(aURL, null, null).get();
        // Fixe l'image sur le composant ImageView
        aView.setImageBitmap(bm);

    } catch (IOException e) {
        aView.setImageDrawable(mNoImage);
        Log.e("DVP Gallery",
                "Erreur t�l�chargement image URL : " + aURL.toString());
        e.printStackTrace();
    }
}
Moustafa Mohamed
  • 226
  • 1
  • 2
  • 9
  • I have a problem here `bm = BitmapFactory.decodeStream(bis);` it said **The final local variable bm cannot be assigned, since it is defined in an enclosing type** – Bilel Sep 27 '13 at 21:17
0

In line with the documentation of AsyncTask you can use something like this:

class param{
    ImageView iv;
    URL aURL;
}

private class MyAsyncTask extends AsyncTask<param, Void, Void> {
     protected Void doInBackground(param... p) {
         int count = p.length;
         for (int i = 0; i < count; i++) {
             // your operations using p.iv and p.aURL
         }
     }

 }

Then you can recall it passing all the parameters you need:

new MyAsyncTask().execute(param1, param2, param3);
GVillani82
  • 17,196
  • 30
  • 105
  • 172