Dim j As HttpJob
j.Initialize("ImgDownload", Me)
j.Download(ServerUrl & "media/" & $Filename)
and, then, I use the following code to get the image:
B4X:
Dim bmp As Bitmap = Job.GetBitmap
However, every once in a while, for a variety of other reasons, the file to be downloaded is missing or has been deleted by some other process. This causes the above Job.GetBitmap to throw an error which I handle and put a "image missing" image into variable bmp.
I'd like to avoid the error entirely - is there a way to test if the Job really has a file or not? I've tried:
Dim links As Map
links.Initialize
links.Put(ImgVw, Starter.ServerUrl & "media/" & $Fiilename)
CallSubDelayed2(ImageDownloader, "Download", links)
which works great, but then when I call:
B4X:
Dim bmp As Bitmap
bmp.Initialize(File.DirAssets, "image_missing.png") 'This is a backup image in case the real image is missing
bmp = ImgVw.Bitmap 'This is the real image
FitCenterBitmap(ImgVw, bmp)
I get an error from the FitCenterBitmap function that variable bmp (the downloaded image) is not initialized - probably because it isn't downloaded yet. I like this method the best, but I must do a re-fit somehow, like FitCenterBitmap, and not sure how to do the re-fit.
The server should respond a 404 error (not found) if the file isn't there. You can do a Job.GetString (this will not harm anything) and check if it contains 404 or so.
As I use php I would check it in the script. The response is always a map (so you can check very easy if the key is f.e. "File" plus the file data as the value or if the key is "Not found" or similar.
@KMatle - I investigated your suggestion and I think that it is on the right path. However...in the ImageDownloader service, I can't figure out how to access the Response. What I mean is that I see the detailed 404 error in the log with a line that looks like this:
And I want to test if the Response string contains string '404' (which it does). But the ImageDownloader uses HttpJob and I can't figure out how to get at the Response or the Reason. Can you advise?
As you can see, it uses Job.Done, too where you can add a
B4X:
IF Job.GetString.Contains("404") then 'not found
ELSE
'do something with the image
END IF
The ImageDownloader just loads existing files (take a look) and has no logic if one isn't available as I can see. So just add it. Easier than it look's.
Please log Job.GetString and copy it here (just right click the logs window and select copy all - no need to upload images ) and we see what the server ist returning in that case.
Yes, I'd like to - I see the 404 in the log in the Response field, but I can't get at the Reason or Response object, though, or retrieve the text. So, it's in the log, but I can't get at it with Job.GetString or any other command.
Yes - the 404 gets logged before JobDone even fires. Here's the code and log file. You'll see in the log file that the "not Success" message comes after the 404 error, even though it's the first thing that happens in JobDone.
Thanks - I will go with that - the GetString method seems to work only on successful downloads (and retrieves the image binary data) but not on unsuccessful downloads (it causes an error). Out of curiosity, is there any way to access the Response and/or Reason property's (pictured in red in this image): (if not, it's okay, just thought that I'd try)
Sub JobDone(Job As HttpJob)
If Job.Success = False Then
Log($"Error occured: ${Job.ErrorMessage}"$)
else If Job.Success Then
End If
Job.Release
End Sub