how to load bitmap directly with picasso library like following

谁都会走 提交于 2019-12-06 17:16:00

问题


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);

回答1:


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);
}



回答2:


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)



回答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




回答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());
    }
}


来源:https://stackoverflow.com/questions/34629424/how-to-load-bitmap-directly-with-picasso-library-like-following

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!