Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to load bitmap directly with picasso library like following

Tags:

java

picasso

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);
like image 783
Sachin Mandhare Avatar asked Jan 06 '16 09:01

Sachin Mandhare


People also ask

How do you load an image into an imageView from an image URL using Picasso?

Image loading using Picasso is very easy, you can do it like this way Picasso. get(). load("http://i.imgur.com/DvpvklR.png").into(imageView); and in their website you can get every details. In your case you can parse every image URL and use RecyclerView to show them along with Picasso.

How to load an image into an image view using Picasso?

In the first line, we get a reference to the ImageView instance in the layout file. We then load an image into the image view using the Picasso library. We first specify the context by calling with () and passing in the context. We then call the load () method and supply in it with the location of the image.

What is Picasso image loader library in Android?

How to Use Picasso Image Loader Library in Android? Picasso is open source and one of the widely used image download libraries in Android. It is created and maintained by Square. It is among the powerful image download and caching library for Android.

How to load an image by URL using Picasso library on Kotlin?

How do I load an Image by URL using Picasso Library on Kotlin? This example demonstrates how to create a WebView in an Android App using Kotlin. Step 1 − Create a new project in Android Studio, go to File ? New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

How do I use Picasso to fetch a remote image?

Here we are using Picasso to fetch a remote image and resize it before displaying it in an ImageView. If your application relies on remote assets, then it’s important to add a fallback in the form of a placeholder image. The placeholder image is shown immediately and replaced by the remote image when Picasso has finished fetching it.


3 Answers

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);
}
like image 111
Vit Avatar answered Sep 23 '22 06:09

Vit


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)
like image 38
ivoroto Avatar answered Sep 24 '22 06:09

ivoroto


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());
    }
}
like image 2
Abdur Rehman Avatar answered Sep 27 '22 06:09

Abdur Rehman