Android Question Crash when gathering email clients

andyp

Member
Licensed User
App was working fine, until an android update.

Now my app crashes when gathering a list of installed email clients

B4X:
Sub GetEmailPackage As String
    Dim PM As PackageManager, DummyIntent As Intent, DummyIntent2 As Intent
    Dim EmailActivities As List, EmailAppNames As List, iPackage As Int, x As Int, tempString As String
    EmailActivities.Initialize: EmailAppNames.Initialize
    
    DummyIntent.Initialize("android.intent.action.SENDTO", "mailto:[email protected]")
    EmailActivities = PM.QueryIntentActivities (DummyIntent)

    If EmailActivities.Size = 0 Then
        DummyIntent2.Initialize("android.intent.action.SENDMULTIPLE", "")
        DummyIntent2.SetType ("message/rfc822")
        EmailActivities = PM.QueryIntentActivities (DummyIntent2)
    End If

    If EmailActivities.Size = 0 Then Return ""

    ' Get app labels & icons
    For x = 0 To EmailActivities.Size - 1
        Dim imgIcon As BitmapDrawable
        Dim sPackageName As String = EmailActivities.Get(x)
        sPackageName = sPackageName.SubString2 (0, sPackageName.IndexOf ("/"))
        tempString = PM.GetApplicationLabel(sPackageName)
        imgIcon = PM.GetApplicationIcon(sPackageName) 'App icon if desired
        EmailAppNames.Add (tempString)
    Next

    iPackage = InputList (EmailAppNames,"Send email using",-1)
    If iPackage = DialogResponse.CANCEL Then Return "/cancel/"

    Return EmailActivities.Get (iPackage)

End Sub

error log:

Send Att: /storage/emulated/0/scripts/EmailedBox.f3a
loadthebox_getemailpackage (java line: 723)
java.lang.ClassCastException: android.graphics.drawable.AdaptiveIconDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
at f3azone.pro.reader.loadthebox._getemailpackage(loadthebox.java:723)
at f3azone.pro.reader.loadthebox._sendemail(loadthebox.java:850)
at f3azone.pro.reader.loadthebox$ResumableSub_Button3_Click.resume(loadthebox.java:545)
at f3azone.pro.reader.loadthebox._button3_click(loadthebox.java:491)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6897)
at android.widget.TextView.performClick(TextView.java:12693)
at android.view.View$PerformClick.run(View.java:26100)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6942)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

Thank you for any help
Andrew
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should run your app in debug mode to see the exact error line. It is here:
B4X:
imgIcon = PM.GetApplicationIcon(sPackageName)

You shouldn't assume that GetApplicationIcon will return a BitmapDrawable. It can return other types of drawables.

A simple solution is to skip other types:
B4X:
Dim o As Object = PM.GetApplicationIcon(sPackageName)
If o Is BitmapDrawable Then
 imgIcon = o
End If

Another option is to draw the drawable with Canvas.DrawDrawable.
 
Upvote 0
Top