如何保存已安装的应用程序图标

时间:2014-02-28 11:06:18

标签: android android-icons android-external-storage

我想在平板电脑中获取所有已安装应用程序的所有图标。我知道如何获取图标以及如何查看它们,但我想将每个图标保存在外部文件中。用于获取图标的代码部分由下面的代码给出。

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

 }

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

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;
}