Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

android - 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 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 :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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);
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...