Loading Image using picasso inside AsyncTask

2019-01-20 06:21发布

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 appearance to my application,

Here is my code :

  private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(MainActivity.this);
        mProgressDialog.setMessage("Chargement...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();

    }

    @Override
    protected Void doInBackground(Void... params) {
           Picasso.with(MainActivity.this).load("http://tv2.orangeadd.com/mediacenter-data/ofc__bg_home.jpg").into(background,new com.squareup.picasso.Callback() {
                 @Override
                 public void onSuccess() {
               mProgressDialog.dismiss();
                 }

                 @Override
                 public void onError() {

                 }
             }); 
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {





    }
    }

this is always showing an error and forcing my application to quit !

Thank's guys :)

3条回答
再贱就再见
2楼-- · 2019-01-20 06:50

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.

查看更多
放我归山
3楼-- · 2019-01-20 07:05
public void loadImageInBackground() {

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

        Target target = new Target() {

            @Override
            public void onPrepareLoad(Drawable arg0) {

                mProgressDialog.show();
            }

            @Override
            public void onBitmapLoaded(Bitmap arg0, LoadedFrom arg1) {

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

            @Override
            public void onBitmapFailed(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);
    }
查看更多
Bombasti
4楼-- · 2019-01-20 07:08

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

public void functionCalledFromUIThread(){

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
             public void onSuccess() {
                mProgressDialog.dismiss();
             }

             @Override
             public void onError() {
                mProgressDialog.dismiss();
             }
         }); 
}
查看更多
登录 后发表回答