2013-08-27 7 views
11

Es gibt eine java.lang.ClassCastException, wenn ich versuche, ein Symbol für installierte Anwendungen zu erhalten.Erhalten Sie alle installierten Anwendungssymbole in Android: java.lang.ClassCastException

Hier ist mein Code.

public Bitmap getAppIcon(String path) { 
     PackageInfo pi = pm.getPackageArchiveInfo(path, 0); 
     pi.applicationInfo.sourceDir = path; 
     pi.applicationInfo.publicSourceDir = path; 
     Drawable icon = pi.applicationInfo.loadIcon(pm); 
     Bitmap APKicon = ((BitmapDrawable) icon).getBitmap(); 
     return APKicon; 
    } 

wo pm ist

PackageManager pm = getPackageManager(); 

und der Fehler in der ist LogCat gezeigt

Caused by: java.lang.ClassCastException: android.graphics.drawable.NinePatchDrawable cannot be cast to android.graphics.drawable.BitmapDrawable 

Hoffnung jemand kann mir helfen.

+0

prüfen dieses Thema bitte http://stackoverflow.com/questions/10623570/how-to-programmatically-re-apply-a-9-patch-image-to -an-imageview –

Antwort

19

Ihr Problem ist, dass Ihr Symbol ist keine Instanz von BitmapDrawable, kann man so überprüfen:

Bitmap APKicon; 
if(icon instanceof BitmapDrawable) { 
    APKicon = ((BitmapDrawable)icon).getBitmap(); 
} 

in diesem Fall Ihre Lösung wäre richtig, kann man wirft es einfach, aber wenn nicht (und das Ihr Fall ist) ein, so etwas tun muss:

else{ 
    Bitmap bitmap = Bitmap.createBitmap(icon.getIntrinsicWidth(),icon.getIntrinsicHeight(), Config.ARGB_8888); 
    Canvas canvas = new Canvas(bitmap); 
    icon.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); 
    icon.draw(canvas); 
    APKicon = bitmap; 
} 

diese Lösung wird in jedem Fall arbeiten

+0

gut! diese Arbeit! Vielen Dank! –

0
public Bitmap getAppIcon(String path) { 
    PackageInfo pi = pm.getPackageArchiveInfo(path, 0); 
    Drawable icon = pi.applicationInfo.loadIcon(pm); 
    Bitmap APKicon = ((NinePatchDrawable) icon).getBitmap(); 
    return APKicon; 
} 

Sie haben das Symbol in NinePatchDrawable (Linie 4) zu werfen.

0
Caused by: java.lang.ClassCastException: android.graphics.drawable.NinePatchDrawable cannot be cast to android.graphics.drawable.BitmapDrawable 

Problem:

Sie versuchen BitmapDrawable zu NinePatchDrawable

Lösung typisieren:

Try this:

public Bitmap getAppIcon(String path) { 
     PackageInfo pi = pm.getPackageArchiveInfo(path, 0); 
     pi.applicationInfo.sourceDir = path; 
     pi.applicationInfo.publicSourceDir = path; 
     Drawable icon = pi.applicationInfo.loadIcon(pm); 
     Bitmap APKicon = ((NinePatchDrawable) icon).getBitmap(); // cast to NinePatchDrawable 
     return APKicon; 
    } 

PackageManager.GetApplicationInfo gibt ein Drawable Objekt. Es könnte kein BitmapDrawable sein.

Sie können testen, ob es sich um eine BitmapDrawable mit ist:

Wenn pm.GetApplicationIcon(packname) Ist BitmapDrawable Then ...

Wenn Sie eine nicht-BitmapDrawable zu einem BitmapDrawable konvertieren wollen, dann müssen Sie:

  • Erstellen Sie eine veränderbare Bitmap.
  • Zeichnen Sie die ziehbar mit Canvas.DrawDrawable
+0

Downvoter Pflege zu kommentieren? –

+1

Ich habe nicht downvote, aber ich sehe die Meldung "Die Methode getBitmap() ist nicht definiert für den Typ NinePatchDrawable" –