簡體   English   中英

如何保存已安裝的應用程序圖標

[英]how to save Installed Applications icons

我想獲得平板電腦中所有已安裝應用程序的所有圖標。 我知道如何獲取圖標以及如何查看它們,但是我想將每個圖標保存在外部文件中。 下面的代碼給出了用於獲取圖標的代碼部分。

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

 }

嘗試這樣的事情:

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