www.999yzc.comandroid Drawable转换成Bitmap失败

( www.999yzc.com )

错误代码:

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.performLaunchActivityActivityThread.java:2325
            at android.app.ActivityThread.handleLaunchActivityActivityThread.java:2390
            at android.app.ActivityThread.access$800ActivityThread.java:151
            at android.app.ActivityThread$H.handleMessageActivityThread.java:1303
            at android.os.Handler.dispatchMessageHandler.java:102
            at android.os.Looper.loopLooper.java:135
            at android.app.ActivityThread.mainActivityThread.java:5257
            at java.lang.reflect.Method.invokeNative Method
            at java.lang.reflect.Method.invokeMethod.java:372
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.runZygoteInit.java:903
            at com.android.internal.os.ZygoteInit.mainZygoteInit.java:698
     Caused by: java.lang.ClassCastException: android.graphics.drawable.VectorDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
            at app.tianxiayou.AppInfoActivity.initAppInfoAppInfoActivity.java:70
            at app.tianxiayou.AppInfoActivity.onCreateAppInfoActivity.java:52
            at android.app.Activity.performCreateActivity.java:5990
            at android.app.Instrumentation.callActivityOnCreateInstrumentation.java:1106
            at android.app.ActivityThread.performLaunchActivityActivityThread.java:2278
            at android.app.ActivityThread.handleLaunchActivityActivityThread.java:2390
            at android.app.ActivityThread.access$800ActivityThread.java:151
            at android.app.ActivityThread$H.handleMessageActivityThread.java:1303
            at android.os.Handler.dispatchMessageHandler.java:102
            at android.os.Looper.loopLooper.java:135
            at android.app.ActivityThread.mainActivityThread.java:5257
            at java.lang.reflect.Method.invokeNative Method
            at java.lang.reflect.Method.invokeMethod.java:372
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.runZygoteInit.java:903
            at com.android.internal.os.ZygoteInit.mainZygoteInit.java:698

方法中的代码

 AppInfo tempInfo=new AppInfo;
        //获取应用名字
        tempInfo.setAppNamepackageInfo.applicationInfo.loadLabelgetPackageManager.toString;
        //ICON
        icondrawable=packageInfo.applicationInfo.loadIcongetPackageManager;
        bitmapDrawable= BitmapDrawable icondrawable;
        tempInfo.setAppIconbitmapDrawable.getBitmap;

求大神指导

VectorDrawable和BitmapDrawable虽然都是继承至Drawable,但是它们两个是不同的类,不能直接强制转换。

从代码中看到题主是想要一个bitmap,那么可以试试这个方法

private void drawableToBitampDrawable drawable
{
    int w = drawable.getIntrinsicWidth;
    int h = drawable.getIntrinsicHeight; 
    Bitmap bitmap = Bitmap.createBitmapw,h,Bitmap.Config.ARGB_8888;
}

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

VectorDrawable Vector的意思是向量
BitmapDrawable

(看完/读完)这篇文章有何感想! www.999yzc.com的分享…

发表评论

电子邮件地址不会被公开。 必填项已用*标注