2014-02-28 95 views
0

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

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

} 
+0

看看這個問題http://stackoverflow.com/questions/649154/save-bitmap-to-location –

+0

你有一個可繪製,所以有什麼問題? – pskink

+0

我想將它保存在一個externalFile中 – zied

回答

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; 
} 
相關問題