Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Picasso to load an image into a bitmap

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.

like image 462
Alex Avatar asked Oct 31 '22 19:10

Alex


1 Answers

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);
like image 192
AndyRoid Avatar answered Nov 02 '22 09:11

AndyRoid