22

I am attempting to use Picasso to get three Bitmap images from a URL

public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState);
  setContentView(R.layout.tab2);
  Drawable d1 = new BitmapDrawable(Picasso.with(Tab2.this).load(zestimateImg1).get());
}

I am getting FATAL EXCEPTION with this code. I suspect it has to do with the fact that this should be done within AsyncTask, but I can't get it to work. If using that is avoidable, I would like to do this without using AsyncTask.

How can I get this code to run without crashing?

If the best way to do this is with AsyncTask, then that solution is ok.

Brian
  • 7,098
  • 15
  • 56
  • 73
  • Look again at the documentation of picasso and see where they call `get()` and how it's intended to be used. – zapl Nov 28 '14 at 01:33
  • 1
    @zapl I find this: `android.graphics.Bitmap get() Synchronously fulfill this request.` I don't understand threads so much :/ – Brian Nov 28 '14 at 01:37
  • I will also accept the `AsyncTask` solution, though i must load three images. I just need this to work lol – Brian Nov 28 '14 at 01:52
  • @Brian Vanover Can you please provide your solution, actuallly i am working on the same problem. – harshita Jul 06 '18 at 10:01
  • I was looking for a way to do it outside the main thread and the snippet in the question helped me. What an irony. – Bertram Gilfoyle Oct 12 '18 at 14:39

5 Answers5

17

None of above worked for me instead this

Handler uiHandler = new Handler(Looper.getMainLooper());
    uiHandler.post(new Runnable(){
        @Override
        public void run() {
            Picasso.with(Context)
                    .load(imageUrl)
                    .into(imageView);
        }
    });

Hope it may be useful for someone

Nikhil
  • 911
  • 15
  • 28
10

You cannot make synchronous requests in the main thread. If you dont want to use an AsyncThread then just use Picasso together with a Target.

Picasso.with(Tab2.this).load(zestimateImg1).into(new Target(...);

I recommend you save a reference to your target like so:

Target mTarget =new Target (...); 

This is because Picasso uses weak references to them and they might be garbage collected before the process is finished.

peter_budo
  • 1,748
  • 4
  • 26
  • 48
user3864005
  • 101
  • 3
4

Just for the record:

Picasso.with(context).load(url).into(new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        Log.i(TAG, "The image was obtained correctly");
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
        Log.e(TAG, "The image was not obtained");
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
        Log.(TAG, "Getting ready to get the image");
        //Here you should place a loading gif in the ImageView
        //while image is being obtained.
    }
});

Source: http://square.github.io/picasso/

onPrepareLoad() is called always after starting the request. from can be "DISK", "MEMORY" or "NETWORK" to indicate where was the image obtained from.

Juan José Melero Gómez
  • 2,742
  • 2
  • 19
  • 36
1

try this:

Handler uiHandler = new Handler(Looper.getMainLooper());
uiHandler.post(new Runnable(){
    @Override
    public void run() {
        Picasso.with(Context)
                .load(imageUrl)
                .into(imageView);
    }
});
1

This is the same answer of Juan José Melero Gómez but with kotlin:

val imageBitmap = Picasso.get().load(post.path_image).into(object: Target {
        override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
            Log.(TAG, "Getting ready to get the image");
             //Here you should place a loading gif in the ImageView
             //while image is being obtained.
        }

        override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {
            Log.e(TAG, "The image was not obtained");
        }

        override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
            Log.i(TAG, "The image was obtained correctly");
        }

    })
Jorge Casariego
  • 21,948
  • 6
  • 90
  • 97