30
Picasso.with(context).load("url").into(imageView);

Here instead of url i want bitmap how can i achieve this. like below-

Picasso.with(context).load(bitmap).into(imageView);
ccpizza
  • 28,968
  • 18
  • 162
  • 169
Sachin Mandhare
  • 700
  • 2
  • 7
  • 20

5 Answers5

23

This should work for you. Use the returned URI with Picasso.

(taken from: is there away to get uri of bitmap with out save it to sdcard?)

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}
Community
  • 1
  • 1
Vit
  • 375
  • 1
  • 6
9

My Kotlin solution

create the bitmap from data

    val inputStream = getContentResolver().openInputStream(data.data)
    val bitmap = BitmapFactory.decodeStream(inputStream)
    val stream = ByteArrayOutputStream()
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream)

IMPORTANT: if you don't need to store the image you can avoid Picasso and load the image right away

    imageView.setImageBitmap(bitmap)

otherwise store the file and load it with Picasso

    val jpegData = stream.toByteArray()

    val file = File(cacheDir, "filename.jpg")
    file.createNewFile()

    val fileOS = FileOutputStream(file)
    fileOS.write(jpegData)
    fileOS.flush()
    fileOS.close()

    Picasso.get().load(Uri.parse(file.path)).into(imageView)
ivoroto
  • 925
  • 12
  • 12
4
private void loadBitmapByPicasso(Context pContext, Bitmap pBitmap, ImageView pImageView) {
    try {
        Uri uri = Uri.fromFile(File.createTempFile("temp_file_name", ".jpg", pContext.getCacheDir()));
        OutputStream outputStream = pContext.getContentResolver().openOutputStream(uri);
        pBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        outputStream.close();
        Picasso.get().load(uri).into(pImageView);
    } catch (Exception e) {
        Log.e("LoadBitmapByPicasso", e.getMessage());
    }
}
Abdur Rehman
  • 1,247
  • 10
  • 13
0

If you have bitmap, then no need to load using picasso, there is simple solution provided by android, you can use below code

imageView.setImageBitmap(bimap);
Vivek Hande
  • 929
  • 9
  • 11
-3

You are using an old version of Picasso.

Update the version in your Gradle file:

implementation 'com.squareup.picasso:picasso:2.71828'

Java:

Picasso.get().load(R.drawable.landing_screen).into(imageView1);
Picasso.get().load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.get().load(new File(...)).into(imageView3);

See more details on the Picasso website

knjk04
  • 115
  • 2
  • 11
Gobu CSG
  • 653
  • 5
  • 7