android Drawable转换成Bitmap失败
ringa_lee
ringa_lee 2017-04-17 14:21:09
0
2
861

错误代码:

08-07 06:42:30.482  28497-28497/app.tianxiayou E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: app.tianxiayou, PID: 28497
    java.lang.RuntimeException: Unable to start activity ComponentInfo{app.tianxiayou/app.tianxiayou.AppInfoActivity}: java.lang.ClassCastException: android.graphics.drawable.VectorDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.ClassCastException: android.graphics.drawable.VectorDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
            at app.tianxiayou.AppInfoActivity.initAppInfo(AppInfoActivity.java:70)
            at app.tianxiayou.AppInfoActivity.onCreate(AppInfoActivity.java:52)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

方法中的代码

 AppInfo tempInfo=new AppInfo();
        //获取应用名字
        tempInfo.setAppName(packageInfo.applicationInfo.loadLabel(getPackageManager()).toString());
        //ICON
        icondrawable=packageInfo.applicationInfo.loadIcon(getPackageManager());
        bitmapDrawable= (BitmapDrawable) icondrawable;
        tempInfo.setAppIcon(bitmapDrawable.getBitmap());

求大神指导

ringa_lee
ringa_lee

ringa_lee

全部回覆(2)
Ty80

VectorDrawable和BitmapDrawable雖然都是繼承至Drawable,但是它們兩個是不同的類,不能直接強制轉換。

從程式碼看到題主是想要一個bitmap,那就可以試試這個方法

private void drawableToBitamp(Drawable drawable)
{
    int w = drawable.getIntrinsicWidth();
    int h = drawable.getIntrinsicHeight(); 
    Bitmap bitmap = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
}
迷茫

引起:java.lang.ClassCastException:android.graphics.drawable.VectorDrawable無法轉換為android.graphics.drawable.BitmapDrawable

VectorDrawable Vector 的意思是提供
BitmapDrawable

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板