Android Question Download image from webserver timing problems

AHilberink

Active Member
Licensed User
Longtime User
Hi,

I want to download from a webserver. This works, but after continueing on a message.

Using OkHttpUtils2 v2.61 library

Part of my source:
B4X:
Sub OphalenLogo
    HTTPInlog.JobName="Logo"
    HTTPInlog.Download(DownloadUrl)
End Sub

B4X:
If(JobStatus.JobName="Logo") Then
           
            Log(DBFilePath)
            Log(HTTPInlog.GetBitmap)
            Log("Ja")

            If(JobStatus.GetBitmap.Width<>0) Then
                ' Thats the name of this special download we created using the Tag-Property
                Dim OutStream As OutputStream
                Log("DownloadReady: "&JobStatus.Tag)
               
                OutStream = File.OpenOutput(DBFilePath, "logo2.jpg", False) ' Job.Tag is read to set the Original Filename we specify earlier in the creation of the Job
                JobStatus.GetBitmap.WriteToStream(OutStream,100,"JPEG")
                OutStream.Close
           
                Dim InStream As InputStream

                InStream = File.OpenInput(DBFilePath,"logo2.jpg")
                Log(InStream.BytesAvailable)
                InStream.Close

                Log("file://"&DBFilePath&"/logo2.jpg")
                Log(JobStatus.Tag&" are written to "&DBFilePath) ' Write the Originalname to the log to see what happens ;-)
            End If
End If

The log indicates:
B4X:
/mnt/sdcard/CIRIS
main_jobdone (java line: 742)
java.lang.RuntimeException: Error loading bitmap.
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:538)
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize(CanvasWrapper.java:516)
    at anywheresoftware.b4a.keywords.Common.LoadBitmap(Common.java:1315)
    at anywheresoftware.b4a.samples.httputils2.httpjob._getbitmap(httpjob.java:165)
    at CIRIS.Chauffeur.main._jobdone(main.java:742)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
    at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1151)
    at android.os.Handler.handleCallback(Handler.java:605)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4340)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Error loading bitmap.

>> CONTINUE = YES <<

/mnt/sdcard/CIRIS
(Bitmap): 523 x 198, scale = 1.00
Ja
DownloadReady: java.lang.Object@4107cc68
58864
file:///mnt/sdcard/CIRIS/logo2.jpg
java.lang.Object@4107cc68 are written to /mnt/sdcard/CIRIS

The >> CONTINUE = YES << is added by me to indicate the error position and the answer.

After YES the file is correctly saved as you can see.

It seems like a timing problem, but I don't know how to resolve this. Filesize only 60k.

Anybody can help?

Best regards,
André
 

AHilberink

Active Member
Licensed User
Longtime User
I don't really understand your code, however if you are using B4A v7+ then you can simplify it with wait for: [B4X] OkHttpUtils2 with Wait For

Hi Erel,

I am using B4A v7.3 and already tried that code you mentioned and didn't succeed. I will give it a second try and let you know.

You didn't understand my code and I see something was missing, the event JobDone so the code should be:
B4X:
Sub JobDone (JobStatus As HttpJob)
    If (JobStatus.Success=True And JobStatus.GetString.Contains("error")<>True) Then
           If(JobStatus.JobName="Logo") Then
           
            Log(DBFilePath)
            Log(HTTPInlog.GetBitmap)
            Log("Ja")

            If(JobStatus.GetBitmap.Width<>0) Then
                ' Thats the name of this special download we created using the Tag-Property
                Dim OutStream As OutputStream
                Log("DownloadReady: "&JobStatus.Tag)
               
                OutStream = File.OpenOutput(DBFilePath, "logo2.jpg", False) ' Job.Tag is read to set the Original Filename we specify earlier in the creation of the Job
                JobStatus.GetBitmap.WriteToStream(OutStream,100,"JPEG")
                OutStream.Close
           
                Dim InStream As InputStream

                InStream = File.OpenInput(DBFilePath,"logo2.jpg")
                Log(InStream.BytesAvailable)
                InStream.Close

                Log("file://"&DBFilePath&"/logo2.jpg")
                Log(JobStatus.Tag&" are written to "&DBFilePath) ' Write the Originalname to the log to see what happens ;-)
            End If
           End If
    Else
        Log("Fout")
        Msgbox("Er is een fout in de communicatie. Programma wordt gestopt.","")
        HTTPInlog.Release
        Activity.Finish
    End If
End Sub

It looks like the JobDone event is fired before the download is completed. Will this be different with the "Wait for"? This will also wait for the JobDone event, will it not? Of course your new code is more simplified :) Mine does have some testing lines.

Best regards,
André
 
Upvote 0
Top