Android Question HttpRequest gives me a strange error

TrisectDevelopment

Active Member
Licensed User
Longtime User
I have an activity in which I load images from the web.
I take the images and add them to a horizontal scrollview

First time i works just fine, but if I fx leave the activity and go to another to view one of the images and go back the images is no longer shown.

If I go back to the previous activity and then select the same activity again, the images is no longer shown.

In my code I have this
B4X:
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   
    Dim b As Bitmap
    b.Initialize2(Response.GetInputStream)
First time the line b.Initialize2(Response.GetInputStream) works, but the second time the program just do nothing. No errors just nothing. It just stops executing the code.
I can still click buttons and so.

I hope there is a clever person who can help me and se what could be wrong!


This is all my code from the activity:

B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: False
#End Region

'**********************************************
'** Process Globals
'**********************************************
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

'**********************************************
'** Globals
'**********************************************
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private pnlOpgaven As Panel
    Private pnlBilledeVideo As Panel
    Private txtBranche As EditText
    Private txtBeskrivelse As EditText
    Private txtHvor As EditText
    Private txtHvornaar As EditText
    Private hsvBillederVideo As HorizontalScrollView
   
    Dim BilledeVideo As Bitmap
    Dim dr As ColorDrawable                        '** Bruges til at sætte bagrundsfarve på paneler
    Dim AfgivBudJob As HttpJob
    Dim URLStreng As String
    Dim hc As HttpClient
    Dim req As HttpRequest
   
    '** String functions
    Dim StFu As StringFunctions
    Dim BilledeListe As List
    Dim BilledeCounter As Int
    Dim BillederBitmaps As List                            '** Denne indeholder de valgte billeder
    Dim BillederBitmapsCounter As Int                    '** Denne tæller de valgte billeder
End Sub

'**********************************************
'** Activity create
'**********************************************
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    '** Show layout
    Activity.LoadLayout("opgavenLayout")

    BilledeListe.Initialize

    hc.Initialize("hc")
   
    dr.Initialize(Colors.White, 0)
    txtBeskrivelse.Background = dr       
    txtBranche.Background = dr
    txtHvor.Background = dr
    txtHvornaar.Background = dr
           
    '** Læg tekster ind i felterne
    Dim splittedString As List
    splittedString = StFu.Split(Functions.ValgtOpgave, "&")   
   
    txtBeskrivelse.Text = splittedString.Get(1)
    txtBranche.Text = splittedString.Get(0)
    txtHvor.Text = splittedString.Get(3)
    txtHvornaar.Text = splittedString.Get(2)
    Functions.ValgtTaskID = splittedString.Get(4)
   
    '** Først gang
    If FirstTime = True Then
        BillederBitmaps.Initialize
        BillederBitmapsCounter = 0
    End If
   
    '** Lav billeder og videoer
    Dim BilledeVideoString As String
    Dim BilledeVideoSplitted As List
    Dim VideoCompareString As String
   
    BilledeVideoString = splittedString.Get(5)
    BilledeVideoSplitted = StFu.Split(BilledeVideoString, "#")
    BilledeCounter = BilledeVideoSplitted.Size
   
    '** Undersøg om den sidste er en film
    VideoCompareString = BilledeVideoSplitted.Get(BilledeCounter-1)
    VideoCompareString = StFu.Mid(VideoCompareString, 2, 10)
    If VideoCompareString = "_movie.mov" Then
        Functions.iVideo = 1
        BilledeCounter = BilledeCounter - 1
       
        '** Læg billeder og video ind
        hsvBillederVideo.Panel.LoadLayout("BillederVideoLayout")
    End If   
   
    '** Gem billeder i en liste
    For i = 0 To BilledeCounter
        BilledeListe.Add(BilledeVideoSplitted.Get(i))
    Next

    '** Hent billeder
    req.InitializeGet("http://94.143.10.158/images/" & BilledeListe.Get(0))
    hc.Execute(req, 1)       

    '** Initializer HTTPJobs
    AfgivBudJob.Initialize("AfgivBudJob", Me)    '** Opret opgaven
End Sub

'**********************************************
'** Activity resume
'**********************************************
Sub Activity_Resume
End Sub

'**********************************************
'** Activity pause
'**********************************************
Sub Activity_Pause (UserClosed As Boolean)
End Sub

'**********************************************
'** Navigations knapper
'**********************************************
Sub btnAnnuller_Click

    '** Spring til menu
    StartActivity(Main)
    Activity.Finish
End Sub

'**********************************************
'** Knapper
'**********************************************
Sub lblAfgivBud_Click

    '** Spring til afgiv bud
    StartActivity(AfgivBud)
    Activity.Finish
End Sub

'**********************************************
'** Klik på billede i scrillview
'**********************************************
Sub ImageView_Click
    Dim b As ImageView
   
    b = Sender
    Functions.iValgtBilledeIndex = b.Tag
   
    '** Der er valgt video
    If b.Tag = 999 Then
        '** Kald activity med parameter
        StartActivity(VisVideo)
        Activity.Finish
    Else '** Der er valgt et billede
        Functions.ValgtBillede = b.Bitmap
   
        '** Kald activity med parameter
        StartActivity(Billeder)
        Activity.Finish
    End If
End Sub

'**********************************************
'** Functions
'**********************************************
Sub AddBillederOgVideo
    Dim width, offsetX, offsetY As Int
    Dim rbmp As RSImageProcessing
   
    width = 75
   
    offsetX = 0
    offsetY = 0

    '** HER PRØVER VI NOGET NYT KODE
    Dim xCount As Int = 0
   
    '** Gennemløb billeder
    For x = 0 To BillederBitmaps.Size - 1
        Dim s As ImageView
       
        s.Initialize("ImageView") 'All SuggarImgView share the same event sub
        s.Color = Colors.Transparent

        rbmp.Initialize       
   
        BilledeVideo = BillederBitmaps.Get(x) 'LoadBitmap(File.DirRootExternal, Functions.iBilleder.Get(x))
        BilledeVideo = rbmp.createScaledBitmap (BilledeVideo, 75, 75, False)
        BilledeVideo = rbmp.rotate(BilledeVideo, 90)
        s.SetBackgroundImage(BilledeVideo)
        hsvBillederVideo.Panel.AddView(s,offsetX + xCount * (width+5), 0, width, width)

        '** sæt index som tag           
        s.Tag = x
       
        '** increment x counter
        xCount = xCount + 1
    Next
   
    hsvBillederVideo.Panel.Width = (xCount+1) * 75
   
    '** Check om der er en video også
    If Functions.iVideo = 1 Then
        Dim s As ImageView
       
        s.Initialize("ImageView") 'All SuggarImgView share the same event sub
        s.Color = Colors.Transparent

        rbmp.Initialize       

        BilledeVideo = LoadBitmap(File.DirAssets, "film.png")
        BilledeVideo = rbmp.createScaledBitmap (BilledeVideo, 75, 75, False)
        s.SetBackgroundImage(BilledeVideo)
        hsvBillederVideo.Panel.AddView(s,offsetX + xCount * (width+5), 0, width, width)

        '** sæt index som tag           
        s.Tag = 999               
       
        '** increment x counter
        xCount = xCount + 1
       
        hsvBillederVideo.Panel.Width = (xCount+1) * 75
    End If
End Sub

'*************************
'** HTTP - Job Done
'*************************
Sub JobDone(Job As HttpJob)
    If Job.Success = True Then
        Select Job.JobName
            '**************************
            '** Opret opgaven
            '**************************
            Case "AfgivBudJob"
                Dim svar As String
               
                '** Læs return svar
                svar = Job.GetString

                '** Find svar mellem <body> og </body>
                svar = StFu.MidExtract(svar, "<body>", "</body>")
               
                '** Split svar
                Dim splittedString As List
                splittedString = StFu.Split(svar, ":")
                               
                If splittedString.Get(0) == "succes" Then
                End If
        End Select
    Else
'        internetConnectionOn = False
        'Log("Error: " & Job.ErrorMessage)
        'ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
End Sub

'*************************
'** HttpClient events
'*************************
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   
    Dim b As Bitmap
    b.Initialize2(Response.GetInputStream)
   
    BillederBitmaps.Add(b)

    BillederBitmapsCounter = BillederBitmapsCounter + 1
   
    If BillederBitmapsCounter < BilledeCounter Then
        req.InitializeGet("http://94.143.10.158/images/" & BilledeListe.Get(BillederBitmapsCounter))
        hc.Execute(req, 1)   
    Else
        BillederBitmapsCounter = 0
        AddBillederOgVideo
    End If

    Response.Release
End Sub

Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
    Log("Error connecting: " & Reason & " " & StatusCode)
    If Response <> Null Then
        Log(Response.GetString("UTF8"))
        Response.Release
    End If
End Sub
 

TrisectDevelopment

Active Member
Licensed User
Longtime User
I found and example on the forum using HTTPClient and used it in my App.

Now I'm using ImageDownloader but I have another problem.
My code is loading the images from the web and adding them to a horizontal scrollview with image size 75x75
Se my code here.
B4X:
    '** Gennemløb billeder
    For x = 0 To BilledeListe.Size - 2
        Dim s As ImageView
      
        s.Initialize("ImageView") 'All SuggarImgView share the same event sub
        s.Color = Colors.Transparent

        rbmp.Initialize      
         
        Dim links As Map
        links.Initialize
  
        links.Put(s, "http://94.143.10.158/images/" & BilledeListe.Get(x))

           CallSubDelayed2(ImageDownloader, "Download", links)
      
        hsvBillederVideo.Panel.AddView(s,offsetX + xCount * (width+5), 0, width, width)

        '** sæt index som tag          
        s.Tag = x
      
        '** increment x counter
        xCount = xCount + 1
    Next

I use Imagedownloader to put images in s and and add s to my scrollview.
BilledeListe.Get(x) contains the image name.

My problem is that I can only se a part of the images, I need them to fit to 75x75
And they are rotated, I need to rotate them to look correct.

Can you help or guide me to an solution?
 
Upvote 0
Top