Android Question Test if Job.GetBitmap really has image data

swabygw

Active Member
Licensed User
Longtime User
I use the following code to download an image:
B4X:
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:
B4X:
If Job.GetBitmap = Null Then ...
But this doesn't work. Is there another way?
 

swabygw

Active Member
Licensed User
Longtime User
I've also tried using the following instead:
B4X:
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.
 
Upvote 0

swabygw

Active Member
Licensed User
Longtime User
I found a solution - go into the ImageDownloader code and use FitCenterBitmap in there. Sorry for the runaround...just had to talk it out a bit. :)
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
I found a solution

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.
 
Upvote 0

swabygw

Active Member
Licensed User
Longtime User
@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:

ResponseError. Reason: Not Found, Response: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">...404....

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?

ImageDownloader here (https://www.b4x.com/android/forum/t...ple-way-to-download-images.30875/#post-179505)
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
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.

I've done a small PHP script which retrieves all the files from a directory on the server: https://www.b4x.com/android/forum/t...r-directory-via-httputils2-php.43320/#content with a complete example. Maybe you are able to use it.
 
Upvote 0

swabygw

Active Member
Licensed User
Longtime User
I tried it in the JobDone in the ImageDownloader like this:

upload_2017-3-12_14-48-47.png

But it never fired the "404" message.
Btw, the "not 404" portion of that code does, indeed, fire.
 
Last edited:
Upvote 0

swabygw

Active Member
Licensed User
Longtime User
Yep, here it is. Thanks

Wait, uploading a second file with the log of Job.GetString
 

Attachments

  • Document1.txt
    6.7 KB · Views: 199
Upvote 0

swabygw

Active Member
Licensed User
Longtime User
Here it is. There are four files being downloaded. The first three download successfully - the fourth one is the one that is the missing image file.
 

Attachments

  • Document1.txt
    13.3 KB · Views: 196
Upvote 0

swabygw

Active Member
Licensed User
Longtime User
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.
 
Upvote 0

swabygw

Active Member
Licensed User
Longtime User
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.

upload_2017-3-14_11-27-54.png
 

Attachments

  • Document1.txt
    17.4 KB · Views: 175
Upvote 0

swabygw

Active Member
Licensed User
Longtime User
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)

upload_2017-3-14_13-46-54.png
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
In case of Success = false you should set a dummy image (a PictureNotFound.jpg for ex)
is there any way to access the Response
B4X:
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
 
Upvote 0
Top