Android Question android.graphics.drawable.AdaptiveIconDrawable cannot be cast to android.graphics.drawable.BitmapDr

Status
Not open for further replies.

dragonguy

Active Member
Licensed User
Longtime User
i try use this code to solve my the AdaptiveIconDrawable, but i got the error at runtime.

code refer at https://www.b4x.com/android/forum/threads/snippet-drawabletobitmap.55759/#post-421275 and https://stackoverflow.com/questions...veicondrawable-to-bitmap-in-android-o-preview
B4X:
Private Sub ShowInstalledApps
    Dim pm As PackageManager  'phone library
    For Each pck As String In pm.GetInstalledPackages
        Dim name As String = pm.GetApplicationLabel(pck)
        Dim a As Object  =pm.GetApplicationIcon(pck)
        If a Is BitmapDrawable Then
            Dim b As BitmapDrawable=a
            lstview.AddTwoLinesAndBitmap(pck,name,b.Bitmap)
        Else
            Dim c As Bitmap = NativeMe.RunMethod("getBitmapFromDrawable",a)
            lstview.AddTwoLinesAndBitmap(pck,name,c)
        End If
    Next
End Sub

#IF JAVA
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

private Bitmap getBitmapFromDrawable(Drawable drawable) {
 
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }

        Bitmap bitmap;
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
}
#End If

error log here
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
anywheresoftware.b4j.object.JavaObject:RunMethod, [getBitmapFromDrawable, 0.0,0.0-144.0,144.0
Loading:3ms
Element:3
Segment:4
Attribute:3]
Error occurred on line: 54 (Main)
java.lang.IllegalArgumentException: method anywheresoftware.b4j.object.JavaObject.RunMethod argument 2 has type java.lang.Object[], got com.samsung.android.graphics.spr.SemPathRenderingDrawable
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:259)
    at b4a.example.main._showinstalledapps(main.java:400)
    at b4a.example.main._activity_create(main.java:392)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at b4a.example.main.afterFirstLayout(main.java:108)
    at b4a.example.main.access$000(main.java:23)
    at b4a.example.main$WaitForLayout.run(main.java:86)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6682)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
attach with testing project.
how to solve this case?
 

Attachments

  • test.zip
    7.9 KB · Views: 364

palang

Member
Licensed User
B4X:
Private Sub ShowInstalledApps
    Dim pm As PackageManager  'phone library
    For Each pck As String In pm.GetInstalledPackages
        Dim name As String = pm.GetApplicationLabel(pck)
        Dim a As Object  =pm.GetApplicationIcon(pck)
        If a Is BitmapDrawable Then
            Dim b As BitmapDrawable=a
            lstview.AddTwoLinesAndBitmap(pck,name,b.Bitmap)
        Else
            Try
               
       
       
                Dim c As Bitmap = NativeMe.RunMethod("drawableToBitmap",Array As Object( a))
            lstview.AddTwoLinesAndBitmap(pck,name,c)
             
            Catch
                Log(LastException)
            End Try
       
        End If
    Next
End Sub

#IF JAVA
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public static Bitmap drawableToBitmap (Drawable drawable) {
    Bitmap bitmap = null;

    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        if(bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }

    if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}
#End If
 
Upvote 0
Status
Not open for further replies.
Top