MBH
MBH

Reputation: 16639

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

I need to load App icon into image view. It is too slow to load it in list view.

I tried to use Picasso or Glide to load it.

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

The function for getting the drawable:

public Drawable getIcon() {
    if (icon == null) {
        icon = getResolveInfo().loadIcon(ctx.getPackageManager());
    }
    return icon;
}

Upvotes: 4

Views: 3052

Answers (3)

David Whitman
David Whitman

Reputation: 378

You can create your own RequestHandler for Picasso. There is a tutorial here.

For example,

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)

Don't forget to scale app icons, by the way! You can't rely on them to be small images and you may end up using a lot more ram than you need.

Upvotes: 0

frusso
frusso

Reputation: 676

You can do this

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 am not sure that in this case the Glide (or Picasso) will be use the cache.

Upvotes: 1

Parag Kadam
Parag Kadam

Reputation: 3850

This one is using the Picasso library.

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

Upvotes: -4

Related Questions