Load Drawable into ImageView using Picasso or Glide or any cashing library - Android

I need to load an application icon into an image. Too slow to load it as a list.

I tried using Picasso or Glide to download it.

I could not find out how to load a Drawable (NOT FROM RESOURCES) into an image view using any of these libraries?

Function to get drawable:

public Drawable getIcon() {
    if (icon == null) {
        icon = getResolveInfo().loadIcon(ctx.getPackageManager());
    }
    return icon;
}
+4
source share
3 answers

You can do it

Drawable icon = ....
Bitmap bitmap = ((BitmapDrawable) icon).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
Glide.with(context)
  .load(bitmapdata)
  .into(imageView);

But I'm not sure if Glide (or Picasso) will use the cache in this case.

+1
source

RequestHandler Picasso. .

,

class AppIconRequestHandler extends RequestHandler {
  @Override
  public boolean canHandleRequest(Request data) {
    return true; // or do validation here
  }

  @Override
  public Result load(Request request, int networkPolicy) {
    // Not sure if DISK or correct or if it should be something else, but it works for me.
    return new Result(yourApp.getIcon().bitmap, Picasso.LoadedFrom.DISK); 
  }
}

// When you want to show the icon
Picasso picasso = Picasso.Builder(context)
            .addRequestHandler(new AppIconRequestHandler())
            .build()

picasso.load(packageName)
            .placeholder(placeholderIcon)
            .into(imageView)

, ! , , , .

0

This one uses the library Picasso.

String url = "some url to your image";
ImageView thumbnail = (ImageView) findViewById(R.id.thumbnail);
Picasso.with(context).load(url).into(thumbnail);
-2
source

Source: https://habr.com/ru/post/1622423/


All Articles