2011-12-17 77 views
2

我想檢索應用程序的圖標。如果我使用獲取應用程序圖標作爲int標識符

String pkg = "com.app.my"; 
Drawable icon = getContext().getPackageManager().getApplicationIcon(pkg); 

結果是可繪製的。我如何檢索相關資源的id,以便將其作爲額外的意圖傳遞給它?謝謝!

回答

4

下面的代碼片段應該做的伎倆。

PackageManager pm = getPackageManager(); 
String pkg = "com.app.my"; 

try { 
    ApplicationInfo ai = pm.getApplicationInfo(pkg, 0); 
    int iconId = ai.icon; 
} catch (NameNotFoundException e) { 
    // ... 
} 
1

使用

getResources().getIdentifier() from your Context (e.g., Activity) 
+0

謝謝,但我不知道可繪製的名稱:P – Addev 2011-12-17 19:10:40

2
   s=name of the drawable 
      int id=getResources().getIdentifier(s,"drawable",getPackageName()); 

      you will get the id here  
+0

謝謝,但我不知道可繪製的名稱:P – Addev 2011-12-17 19:10:47

1

您是指其他應用程序的圖標或您所在的應用程序?

如果它在應用程序中,那麼這應該足夠了嗎?

int i = R.drawable.icon;