1

I'm using Picasso to load an Image from a URL, After I run the application I get to see that "Application may be doing too much work on its main thread". To set the Image I'm writing some thing like this :

Picasso.with(ScrollingActivity.this).load(photoUrl).networkPolicy(NetworkPolicy.OFFLINE).into(userProfileImageView, new Callback() {
            @Override
            public void onSuccess() {

            }

            @Override
            public void onError() {
                with(ScrollingActivity.this).load(photoUrl).into(userProfileImageView);
            }
        });

Now I'm thinking to use AsyncTask to set the Image into ImageView and for that I modified my code to something like :

But now I don't know what to write in doInBackground method and onPostExecute method.

I used the AsyncTask method like this :

class SetImageTask extends AsyncTask<Void, Void, Void>
        {

            @Override
            protected Void doInBackground(Void... voids) {

                Picasso.with(ScrollingActivity.this).load(photoUrl).networkPolicy(NetworkPolicy.OFFLINE).into(userProfileImageView, new Callback() {
                    @Override
                    public void onSuccess() {

                    }

                    @Override
                    public void onError() {
                        with(ScrollingActivity.this).load(photoUrl).into(userProfileImageView);
                    }
                });


                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
            }
        }

But the problem is that I'm setting the Image into the ImageView in the doInBackground method. So I think that needs to be done in UI thread. Can you please tell me how do I set the Image using AsyncTask

Taisbevalle
  • 246
  • 1
  • 9
  • 19
Ayushi bhardwaj
  • 441
  • 5
  • 18
  • Try the regular way. `Picasso.with(ScrollingActivity.this).load(photoUrl).into(userProfileImageView);` and without asynctask. – Enzokie Sep 02 '16 at 15:27
  • The regular way may be causing heavy work in the main UI, so I thing thats the reason I'm getting "Application may be doing too much work on UI thread". I even have many more Images to load using Picasso so that's way I was thing to use AsyncTask – Ayushi bhardwaj Sep 02 '16 at 15:31
  • Why are you using networkPolicy(NetworkPolicy.OFFLINE) ? – Pravin Divraniya Sep 02 '16 at 15:36
  • to load the data offline if the data is present offline – Ayushi bhardwaj Sep 02 '16 at 15:39
  • Simply just write your onPostExecute or remove AsyncTask and write direct code. – Rahul Mandaliya Sep 02 '16 at 15:44
  • 1
    Picasso has its own Asynchronous mechanism already, I would think that the issue is caused by other factor. – Enzokie Sep 02 '16 at 15:46
  • @Enzokie Thanks .. – Ayushi bhardwaj Sep 02 '16 at 15:50
  • If you see this in logcat: `Application may be doing too much work on its main thread`, that can be normal. I can see this even with simplest apps, because every app is doing a lot of work initializing the application when it starts. Picasso is pretty optimized, so I wouldn't try to overengineer it. – JoKr Sep 02 '16 at 15:51

2 Answers2

2

You don't need to use AsyncTask or and background Thread to load image using Piccaso. Picasso is already optimized. It has its own Asynchronous calls. The problem that you are getting

"Application may be doing too much work on its main thread"

is not due to the Picasso. You can refer this link to see reason behind "Application may be doing too much work on its main thread"

Community
  • 1
  • 1
Amit Upadhyay
  • 7,179
  • 4
  • 43
  • 57
0

Use Target then you don't need an AsyncTask as it will decode in the background.

private Target target = new Target() {
    @Override
      public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
         //do whatever you need in here      
      }

    @Override
      public void onBitmapFailed() {
      }
}

private void loadBitmap() {
   Picasso.with(this).load("url").into(target);
}

Make sure you're binding the Target to a view otherwise it can be garbage collected before the process is complete.

Pztar
  • 4,274
  • 6
  • 33
  • 39