Rakhiii
Rakhiii

Reputation: 49

Loading Drawable object in to image View using universal image loader or picasso any one

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 universal image loader 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: Drawable icon = p.applicationInfo.loadIcon(getPackageManager());

Upvotes: 0

Views: 312

Answers (1)

Cătălin Florescu
Cătălin Florescu

Reputation: 5158

Using Picasso you need to convert your drawable/bitmap to a file. Picasso can load image files.

To convert Bitmap/Drawable to file use this. Just change parameter from Bitmap to Drawable.

public static File bitmapToFile(Bitmap bitmap, String fileName) {
    try {
        File file = File.createTempFile(fileName, ".png");
        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();
        file.deleteOnExit();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, bos);
        byte[] bitmapData = bos.toByteArray();
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(bitmapData);
        fos.flush();
        fos.close();
        return file;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

And to load you could use this:

File bitmapFile = bitmapToFile(yourBitmap, "aRandomName");
Picasso.with(context).load(bitmapFile).into(imageView);

Upvotes: 0

Related Questions