Android Question Out of Memory after changing the screenorientation...

schimanski

Well-Known Member
Licensed User
Longtime User
Is there someone who knows, why I always get an out of memory exception, when I change the screenorientation six or seven times? My device is an Galaxy S5. The following log in unfiltered, but I'm not able to see the problem:(

B4X:
** Activity (konferenz) Resume **
Downsampling image due to lack of memory.
Downsampling image due to lack of memory: 2
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
AsyncLoad failed: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
** Activity (konferenz) Pause, UserClosed = false **
** Activity (konferenz) Create, isFirst = false **
(Konferenz, 94) Object was already initialized. (warning #1003)
** Activity (konferenz) Resume **
Downsampling image due to lack of memory.
Downsampling image due to lack of memory: 2
AsyncLoad failed: Out of Memory
** Activity (konferenz) Pause, UserClosed = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
java.lang.OutOfMemoryError
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:683)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:513)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:889)
    at android.content.res.Resources.loadDrawable(Resources.java:3454)
    at android.content.res.Resources.getDrawable(Resources.java:1918)
    at com.android.internal.widget.ActionBarView.setIcon(ActionBarView.java:1037)
    at com.android.internal.policy.impl.PhoneWindow.setDefaultIcon(PhoneWindow.java:1550)
    at android.app.Activity.initActionBar(Activity.java:2017)
    at android.app.Activity.setContentView(Activity.java:2052)
    at de.eis_desktop.eis.main.onCreate(main.java:57)
    at android.app.Activity.performCreate(Activity.java:5451)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2359)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2453)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4036)
    at android.app.ActivityThread.access$1000(ActivityThread.java:173)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5579)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
    at dalvik.system.NativeStart.main(Native Method)

Thanks for help....
 

schimanski

Well-Known Member
Licensed User
Longtime User
Here is my code....It seems, that I make something fundamentally false....:(
Thanks for your efforts

B4X:
'Activity module
Sub Process_Globals
   
    Type Dokumententyp (Name, Groesse, Zeitstempel, Text, Absender As String)

    Dim eis_icon, maps_icon, kamera_icon, anhang_icon, kontakt_icon, gruppen_icon As Bitmap
    Dim gesendet_icon, nichtgesendet_icon As Bitmap
    Dim GPS1 As GPS
   
    Dim ListeDokumente, ListeDokumenteDevice As List
   
    Private Contentchooser1, Contentchooser2 As ContentChooser
   
    Dim BreiteAnhangMenu As Int
   
    Private frontCamera As Boolean = False
   
    Dim StatusKamera As Boolean
    StatusKamera=False
   
    Dim Speicher_Dokumentenname As String
   
    Dim Zeitstempel As Long
   
    Dim Verbindungskontrolle As ServerSocket

End Sub

Sub Globals
    Dim SuchFeld2 As AutoCompleteEditText
   
    Dim dlgFileExpl As ClsExplorerForULV 
   
    Dim StandortDialog1 As InputDialog
    Dim LocationSpeicher1 As Location
   
    Dim ULV As UltimateListView
    Dim lblMeasure As Label
    Dim PanelTexteingabe As Panel
    Dim Texteingabe As EditText
    Dim btnSend As Button

    Dim VerticalPadding As Int
   
    Private ImageView1 As ImageView
   
    Private bar As StdActionBar
    Private vp As StdViewPager
   
    Dim IME As IME

    Dim BP As ClsBitmapProcessor
    Dim ErrorBmp As Bitmap
   
    Dim AnhangMenu As MLmenu
   
    Private PanelCamera As Panel
    Private KameraAusloeser, KameraSwitch, KameraBild As ImageView
    Private camEx As CameraExClass

    Private HintergrundDialog As CustomDialog2
    Private RadioGalerie As RadioButton
    Private RadioKein As RadioButton
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Try
        init_icons       
        Init                   

        Init_ULV                   
       
        dlgFileExpl.Initialize(Activity, "/mnt/sdcard", "", True, False, "OK")
        Init_FileDialog           
       
        Init_HintergrundDialog   
       
        Init_Grafik   
        Init_AnhangMenu           
       
        BP.Initialize
        ErrorBmp = BP.ProcessBitmap(LoadBitmap(File.DirAssets, "fehler.png"), 70dip, False, False, False)
       
        If FirstTime Then GPS1.Initialize("GPS1")
        If FirstTime Then Contentchooser1.Initialize("ContentChooser1")
        If FirstTime Then Contentchooser2.Initialize("ContentChooser2")
        If FirstTime Then Verbindungskontrolle.Initialize(0,"")
       
        IME.Initialize("IME")
        IME.AddHeightChangedEvent
        IME_HeightChanged(100%y, 0)
        IME.AddHandleActionEvent(Texteingabe)
       
    Catch
       
    End Try
End Sub

'I N I T I A L I S I E R U N G'
Sub Init
    DateTime.DateFormat="dd.MM.yyyy"
    DateTime.TimeFormat="HH:mm:ss"
    ListeDokumente.Initialize
    ListeDokumenteDevice.Initialize
    LocationSpeicher1.Initialize 
    ImageView1.Initialize("")
End Sub

'Initialisierungen'
Sub Init_ULV
    Try
        lblMeasure.Initialize("")
        lblMeasure.Background = LoadNinePatchDrawable("balloon_left_" & Main.Theme)
        lblMeasure.TextSize = 15
        lblMeasure.Typeface = Typeface.DEFAULT
        lblMeasure.Visible = False
        Activity.AddView(lblMeasure, 0, 0, 85%x, 100%y)

        Dim r As Reflector, DrwRect As Rect
        r.Target = lblMeasure.Background
        DrwRect.Initialize(0, 0, 0, 0)
        r.RunMethod4("getPadding", Array As Object(DrwRect), Array As String("android.graphics.Rect"))
        VerticalPadding = DrwRect.Top + DrwRect.Bottom
       
        Dim cd As ColorDrawable
        cd.Initialize(Colors.ARGB(255, 51, 181, 229), 0)
       
        'Ultimate Listview initialisieren'
        ULV.Initialize(0, 0, "", "ULV")
        ULV.Color=Colors.LightGray
        ULV.NumberOfAsyncLoaders = 2
        ULV.SetLoadImageCallbacks("", "", "Bmp_PostProcessor", "Bmp_LoadingError")
        ULV.PressedDrawable=cd
        ULV.StackFromBottom(True)
        ULV.TranscriptMode = ULV.TRANSCRIPT_NORMAL
        ULV.DividerHeight=10  'Abstand der einzelnen Nachrichten untereinander'
       
        PanelTexteingabe.Initialize("")
        PanelTexteingabe.Color=Colors.ARGB(255, 225, 225, 225)
       
        'Texteingabe initialisieren'
        Texteingabe.Initialize("Texteingabe")
        Texteingabe.Gravity = Gravity.TOP + Gravity.LEFT
        Texteingabe.SingleLine = False
        Texteingabe.TextColor=Colors.Black
        Texteingabe.TextSize = lblMeasure.TextSize
        Texteingabe.Hint = "Nachricht eingeben"
       
        'Sendebutton initialisieren'
        btnSend.Initialize("btnSend")
        btnSend.Gravity=Gravity.CENTER
        btnSend.SetBackgroundImage(LoadBitmap(File.DirAssets,"ic_action_send_now_" & Main.Theme & ".png"))
    Catch
       
    End Try
End Sub

Sub Init_FileDialog
    dlgFileExpl.DialogRect.Left = 0
    dlgFileExpl.DialogRect.Right = 100%x
    dlgFileExpl.DialogRect.Top = 0
    dlgFileExpl.DialogRect.Bottom = 100%y
    dlgFileExpl.BackgroundColor = Colors.LightGray
    dlgFileExpl.BorderColor = Colors.RGB(128, 128, 128)
    dlgFileExpl.FolderTextColor = Colors.Black
    dlgFileExpl.FileTextColor1 = Colors.Black
    dlgFileExpl.FileTextColor2 = Colors.White
    dlgFileExpl.DividerColor = Colors.Gray
    dlgFileExpl.FolderTextColor = Colors.Black
End Sub

'HintergrundDialog initialisieren'
Sub Init_HintergrundDialog
    Activity.Color=Colors.ARGB(0,0,0,0)
    Dim PanelHintergrund As Panel
    PanelHintergrund.Initialize("")
    HintergrundDialog.AddView(PanelHintergrund, 260dip,115dip)
    PanelHintergrund.LoadLayout("HintergrDialog")
End Sub

Sub init_icons
    anhang_icon.Initialize(File.DirAssets,"ic_action_attachment_" & Main.Theme & ".png")
    eis_icon.Initialize(File.DirAssets,"eis_icon_" & Main.Theme & ".png")
    maps_icon.Initialize(File.DirAssets,"maps.png")
    kamera_icon.Initialize(File.DirAssets,"ic_action_download_" & Main.Theme & ".png")
    kontakt_icon.Initialize(File.DirAssets,"ic_action_person_" & Main.Theme & ".png")
    gruppen_icon.Initialize(File.DirAssets,"ic_action_group_" & Main.Theme & ".png")
   
    gesendet_icon.Initialize(File.DirAssets,"ic_action_gesendet_" & Main.Theme & ".png")
    nichtgesendet_icon.Initialize(File.DirAssets,"ic_action_nichtgesendet_" & Main.Theme & ".png")
End Sub

Sub Init_Grafik
    Try
        Dim height As Int = CalculateHeight(False, True)
        vp.Initialize("vp",1 , 100%x, height)
        Activity.AddView(vp.AsView, 0, 0, 100%x, height)

        vp.Panels(0).AddView(ULV, 0,0,100%x, 100%y-55dip)
        bar.Initialize("bar")
        bar.Icon = LoadBitmap(File.DirAssets, "eis_icon_" & Main.Theme & ".png")
        bar.NavigationMode = bar.NAVIGATION_MODE_STANDARD
       
        bar.ShowUpIndicator = True
        Activity.Title = Main.Konferenznummer
        bar.Subtitle = ""
       
        Activity.AddMenuItem3("Anhang senden", "Button5", LoadBitmap(File.DirAssets, "ic_action_new_attachment_" & Main.Theme & ".png"), True)
       
        SuchFeld2.Initialize("Suchfeld2")
        Activity.AddView(SuchFeld2, 0,0,75%x, 40dip)
        SuchFeld2.Color=Colors.Gray
        SuchFeld2.HintColor=Colors.LightGray
        SuchFeld2.Visible=False
       
        Activity.AddView(PanelTexteingabe, 0, 100%y-55dip, 100%x, 55dip)
        Activity.AddView(Texteingabe, 0, 100%y-53dip, 100%x - 70dip, 55dip)
        Activity.AddView(btnSend, Texteingabe.Width+15dip, 100%y-47dip, 40dip, 40dip)
       
        PanelCamera.Initialize("PanelCamera")
        PanelCamera.Color=Colors.White
        Activity.AddView(PanelCamera, 0,0, 100%x, 100%y)
        PanelCamera.Visible=False
        KameraAusloeser.Initialize("KameraAusloeser")
        KameraAusloeser.Gravity=Gravity.FILL
        KameraAusloeser.Bitmap=LoadBitmap(File.DirAssets, "ic_action_camera2.png")
        Activity.AddView(KameraAusloeser, 50%x-30dip, 90%y-25dip, 60dip, 50dip)
        KameraAusloeser.Visible=False
       
        KameraSwitch.Initialize("KameraSwitch")
        KameraSwitch.Gravity=Gravity.FILL
        KameraSwitch.Bitmap=LoadBitmap(File.DirAssets, "ic_action_switch_camera.png")
        Activity.AddView(KameraSwitch, 75%x-25dip, 85%y-20dip, 50dip, 40dip)
        KameraSwitch.Visible=False
       
        KameraBild.Initialize("KameraBild")
        KameraBild.Gravity=Gravity.FILL
        KameraBild.Bitmap=Null
        Activity.AddView(KameraBild, 5%x, 5%y, 70dip, 70dip)
        KameraBild.Visible=False
    Catch

    End Try
End Sub

Sub Init_AnhangMenu
    AnhangMenu.Initialize(6,40dip,Activity,Me,Colors.White,Colors.Black,16,"AnhangMenu")
    AnhangMenu.AddMenuItem("Bild",LoadBitmap(File.DirAssets, "ic_action_picture_" & Main.Theme & ".png"))
    AnhangMenu.AddMenuItem("Kamera", LoadBitmap(File.DirAssets, "ic_action_camera_" & Main.Theme & ".png"))
    AnhangMenu.AddMenuItem("Video",LoadBitmap(File.DirAssets, "ic_action_video_" & Main.Theme & ".png"))
    AnhangMenu.AddMenuItem("Audio",LoadBitmap(File.DirAssets,"ic_action_mic_" & Main.Theme & ".png"))
    AnhangMenu.AddMenuItem("Datei",LoadBitmap(File.DirAssets, "ic_action_attachment2_" & Main.Theme & ".png"))
    AnhangMenu.AddMenuItem("Standort",LoadBitmap(File.DirAssets, "ic_action_location_found_" & Main.Theme & ".png"))
End Sub
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Hello Erel!

I have made it and it is a little bit better, but alwasy a problem.

I think, that there is also something wrong in my Activity_Resume Sub. The code with the backgroundimage of my ultimatelistview takes a lot of memory, but I don't know how to change the code to set the background at runtime...
B4X:
If File.Exists(File.DirInternal, "Hintergrund.txt")=True Then
    Dim HintergrundPfad As String
    HintergrundPfad=File.ReadString(File.DirInternal, "Hintergrund.txt")
    If File.Exists("", HintergrundPfad)=True Then
       Dim bd As BitmapDrawable
       bd.Initialize(LoadBitmap("", HintergrundPfad))
       bd.Gravity = Gravity.CENTER
       ULV.Background = bd
    End If
End If

B4X:
Sub Activity_Resume
    
        Dim dd As DownloadData
        dd.Verz=Main.Verzeichnis
        dd.Konferenznr=Main.Konferenznummer
        ListeDokumenteSD_erstellen(dd)
   
        Activity.Title=dd.Konferenznr 
   
        'Menüfunktionen initialisieren'
        If dd.Verz="Konferenzen" Then
            Activity.AddMenuItem("Aktualisieren","Aktualisieren")
        End If
        Activity.AddMenuItem("Dokument suchen","Suchen")
        If dd.Verz="Kontakte" Then
            Activity.AddMenuItem("Von SD-Karte löschen","SDErase")
        End If
        If dd.Verz="Konferenzen" Then
            Activity.AddMenuItem("Von Server löschen","ServerErase")
            Activity.AddMenuItem("Kontaktgruppe","Kontaktgruppe")
        End If
        Activity.AddMenuItem("Ton ein/aus","Lautlos")
        Activity.AddMenuItem("Hintergrund","Hintergrund")
        Activity.AddMenuItem("Standorte", "Standorte")
 
        If dd.Verz="Konferenzen" Then
            If Main.Konferenzbild.IsInitialized=True Then
                If Main.Konferenzbild.ContainsKey(dd.Konferenznr)=True Then
                    If File.Exists(File.DirRootExternal & "/EIS/" & dd.Verz & "/" & dd.Konferenznr, Main.Konferenzbild.Get(dd.Konferenznr))=True Then
                        bar.Icon=LoadBitmapSample(File.DirRootExternal & "/EIS/" & dd.Verz & "/" & dd.Konferenznr, Main.Konferenzbild.Get(dd.Konferenznr), 43dip, 43dip)
                    Else
                        bar.Icon=gruppen_icon
                    End If
                Else
                    bar.Icon=gruppen_icon
                End If
            Else
                Main.Konferenzbild.Initialize
                bar.Icon=gruppen_icon
            End If
   
            If Main.Kontaktgruppe.IsInitialized=False Then
                If File.Exists(File.DirInternal, "KG_" & dd.Konferenznr & ".eis")=True AND File.Size(File.DirInternal, "KG_" & dd.Konferenznr & ".eis") > 0 Then
                    Dim Kontaktgruppendatei As RandomAccessFile
                    Kontaktgruppendatei.Initialize(File.DirInternal, "KG_" & dd.Konferenznr & ".eis", False)
                    Main.Kontaktgruppe = Kontaktgruppendatei.ReadEncryptedObject(Main.CopyrightString,0)
                    Kontaktgruppendatei.Close
                Else
                    Main.Kontaktgruppe.Initialize
                End If
            End If
            '-2, weil innerhalb der Kontaktgruppe ein Feld für gesschlossene/offene Konferenz und Löschung vorgehalten wird'
            bar.Subtitle=(Main.Kontaktgruppe.Size-2) & " Kontakte"
        Else
            bar.Icon=kontakt_icon
            bar.Subtitle=""
        End If
  
        ULV.RefreshContent
   
        If Main.Verteiler=True Then
            Main.Verteiler=False
            If dd.Verz="Kontakte" Then
                If Msgbox2("Mit " & dd.Konferenznr & " teilen?", "EIS", "OK", "Abbrechen", "", Null)=DialogResponse.POSITIVE Then
                    Dim su As StringUtils
                    Main.VerteilerPfad=su.DecodeUrl(Main.VerteilerPfad, "UTF8")
                    Dokument_senden
                End If
            Else
                If Msgbox2("In die Konferenz " & dd.Konferenznr & " einstellen?", "EIS", "OK", "Abbrechen", "", Null)=DialogResponse.POSITIVE Then
                    Dim su As StringUtils
                    Main.VerteilerPfad=su.DecodeUrl(Main.VerteilerPfad, "UTF8")
                    Dokument_senden
                End If
            End If
            Main.VerteilerPfad=""
        End If
      
        PushService.Notification1.Cancel(1)
   
        If File.Exists(File.DirInternal, "Hintergrund.txt")=True Then
            Dim HintergrundPfad As String
            HintergrundPfad=File.ReadString(File.DirInternal, "Hintergrund.txt")
            If File.Exists("", HintergrundPfad)=True Then
                Dim bd As BitmapDrawable
                  bd.Initialize(LoadBitmap("", HintergrundPfad))
                  bd.Gravity = Gravity.CENTER
                  ULV.Background = bd
            End If
        End If
  
        'Kamera einschalten, wenn sie vor der Bildschirmdrehung an war'
        If StatusKamera=True Then KameraEin_Click

        'Status immer laden, wenn Activity Konferenz gestartet wird und ein Kontakt ausgewählt wurde"
        If dd.Verz="Kontakte" Then CallSubDelayed2(LadeService, "Status_laden", dd)

End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You shouldn't create any UI in Activity_Resume. Otherwise it will be added multiple times if the user presses on the home button and then returns to your app.

Your current code doesn't cache the images. First change LoadBitmap with LoadBitmapSample. Secondly only load the bitmaps in Activity_Create when FirstTime is true.
 
Upvote 0
Top