Skip to content Skip to sidebar Skip to footer

Loading Image Using Picasso Inside Asynctask

I'm using picasso to load an image as a background for my activity, I want to use an AsyncTask, while the image is loading, when done the progress bar dismisses to give better appe

Solution 1:

publicvoidloadImageInBackground() {

        mProgressDialog = newProgressDialog(MainActivity.this);
        mProgressDialog.setMessage("Chargement...");
        mProgressDialog.setIndeterminate(false);

        Target target = newTarget() {

            @OverridepublicvoidonPrepareLoad(Drawable arg0) {

                mProgressDialog.show();
            }

            @OverridepublicvoidonBitmapLoaded(Bitmap arg0, LoadedFrom arg1) {

                background.setImageBitmap(arg0);
                mProgressDialog.dismiss();
            }

            @OverridepublicvoidonBitmapFailed(Drawable arg0) {
                // TODO Auto-generated method stub
                mProgressDialog.dismiss();
            }
        };

        Picasso.with(MainActivity.this)
                .load("http://tv2.orangeadd.com/mediacenter-data/ofc__bg_home.jpg")
                .into(target);
    }

Solution 2:

You get error because picasso's load function is already async. So you can do this in UI thread like:

publicvoidfunctionCalledFromUIThread(){

mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setMessage("Chargement...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
Picasso.with(MainActivity.this).load("http://tv2.orangeadd.com/mediacenter-data/ofc__bg_home.jpg").into(background,new com.squareup.picasso.Callback() {
             @Override
             publicvoidonSuccess() {
                mProgressDialog.dismiss();
             }

             @Override
             publicvoidonError() {
                mProgressDialog.dismiss();
             }
         }); 
}

Solution 3:

My guess is that the error is because you are trying to modify an UI element (dialog) inside a background thread, which is not possible.

You don't need an AsyncTask for this, since Picasso already does the decoding in background.

Post a Comment for "Loading Image Using Picasso Inside Asynctask"