0

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

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

 }
4

1 回答 1

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;
}
于 2014-02-28T11:17:48.540 回答