cari

Rumah  >  Soal Jawab  >  teks badan

android Drawable转换成Bitmap失败

错误代码:

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_leeringa_lee2772 hari yang lalu859

membalas semua(2)saya akan balas

  • PHP中文网

    PHP中文网2017-04-17 14:23:09

    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);
    }

    balas
    0
  • 迷茫

    迷茫2017-04-17 14:23:09

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

    VectorDrawable Vector的意思是向量
    BitmapDrawable

    balas
    0
  • Batalbalas