简体   繁体   English

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

[英]how to save Installed Applications icons

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)
 {

 }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM