0

I would like to get all the icons of all installed Applications in my Tablet. I know how to get icon and how to view them, but I would like to save each icon in an external file. The part of code used to get icon is given by the code below.

try{
String pkg = "com.app.my";//your package name
Drawable icon = getContext().getPackageManager().getApplicationIcon(pkg);
imageView.setImageDrawable(icon);
}
catch (PackageManager.NameNotFoundException ne)
 {

 }
zied
  • 201
  • 3
  • 7
  • 17

1 Answers1

0

Try something like this:

try{
    //get icon from package
    String pkg = "com.app.my";//your package name
    Drawable icon = getContext().getPackageManager().getApplicationIcon(pkg);
    imageView.setImageDrawable(icon);
    Bitmap bitmap = drawableToBitmap(icon);

    //save bitmap to sdcard
    FileOutputStream out;
    try {
           out = new FileOutputStream(Environment.getExternalStorageDirectory()
                            + File.separator + "output.png");
           bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
            //close output stream (important!)
            try{
                out.close();
            } catch(Throwable ignore) {}
    } 
} catch (PackageManager.NameNotFoundException ne) {}

//convert drawable to a bitmap
public static Bitmap drawableToBitmap (Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable)drawable).getBitmap();
    }

    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap); 
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}
Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76