5

I`m tring to to load an image from an URL into a Bitmap using the Picasso library , but most of the examples i found refer to loading a Bitmap to a ImageView or something similar.

The code should be something like this , according to the documentation.

public void loadImage() {

        Picasso.with(getBaseContext()).load("image url").into(new Target() {

            @Override
            public void onPrepareLoad(Drawable arg0) {
            }
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom arg1) {
                Bitmap bitImage = Bitmap(getApplicationContext(),bitmap);
            }
            @Override
            public void onBitmapFailed(Drawable arg0) {
            }
        });
    }

But Bitmap bitImage = Bitmap(getApplicationContext(),bitmap); doesn't seem to be correct, since i`m getting a Method call expected error.

Alex
  • 127
  • 3
  • 12

1 Answers1

2

It looks like you are not creating the Bitmap properly, but if I was in your position I would create a scaled bitmap like so:

public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);

    // "RECREATE" THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(
    bm, 0, 0, width, height, matrix, false);
    bm.recycle();
    return resizedBitmap;
}

Then set it to an imageView like so:

mImg.setImageBitmap(img);

Overall it would look like this:

public void loadImage() {

    Picasso.with(getBaseContext()).load("image url").into(new Target() {
            // ....

            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom arg1) {
                // Pick arbitrary values for width and height
                Bitmap resizedBitmap = getResizedBitmap(bitmap, newWidth, newHeight);
                mImageView.setBitmap(resizedBitmap);
            }

            // ....
        });
    }
}

public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);

    // "RECREATE" THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(
    bm, 0, 0, width, height, matrix, false);
    bm.recycle();
    return resizedBitmap;
}

But I question you using Target altogether, usually that is for a very specialized case. You should be calling the singleton of Picasso in the same class you will be displaying images. Usually this is in an Adapter (RecyclerView Adapter maybe) like so:

Picasso.with(mContext)
    .load("image url")
    .into(mImageView);
AndyRoid
  • 5,062
  • 8
  • 38
  • 73
  • Thanks, i already have methods in place for resizing (http://developer.android.com/training/displaying-bitmaps/load-bitmap.html) . I just needed to load an image from an URL into a Bitmap ( not directly into an ImageView), figured it out from the comments above. Thanks a lot. – Alex Sep 14 '15 at 00:36
  • Why are you using an asynchronous task to load the image. You can just implement a callback in the .into(mImageView, new Callback<> { ... }); – AndyRoid Sep 14 '15 at 00:38
  • Actually, i was trying a quick way to load an image into a bitmap which will then be indeed passed to a Adapter (RecycleView) , instead of loading it from the resources folder. I'm trying to debug an issue described here http://stackoverflow.com/questions/32554358/encountering-lag-when-updating-a-cardview-item-in-a-recycleview . I'll probably try loading the image in the class where it will be displayed. But i though Picasso could only be used in an activity class, or at least that's what i understood from some posts. And i presume i need to pass Context to the Adapter class , right? – Alex Sep 14 '15 at 00:57
  • Holding onto `Bitmap`s is very memory expensive, especially if you have to load it from a URL. Just let `Picasso` handle it for you like I described above, and yes pass the context into the adapter. – AndyRoid Sep 14 '15 at 00:58
  • Ok, got it, i`ll try it this way, thanks a lot for the assistance. – Alex Sep 14 '15 at 01:02
  • isn't Picasso should do all bitmap processing (for memory optimization) ?? – user924 Sep 17 '17 at 14:17