Download picture from web to file - problem?

Andras

Active Member
Licensed User
Longtime User
I need to download a jpg from an online source to the Files folder of an app, and have been using this code:

B4X:
          If HttpUtils.IsSuccess(Dlfilename) Then
                    In = HttpUtils.GetInputStream(Dlfilename)
                    
                    Out = File.OpenOutput(File.DirDefaultExternal, Datafile, False)
                     File.copy2(In,Out )
                    Out.Flush
                    Out.Close
            
               ToastMessageShow("Picture downloaded OK!", False)
                Else
                    ToastMessageShow("Problem downloading picture!", False)
                End If

However, there's something wrong here and I don't know what; the picture does indeed load to the appropriate location, but shows up as faulty (although I can see it with a viewer!) and the app denies its existence.

I've done similar things to this before with no problems, but am clearly doing something wrong this time! But what?

Thanks! John
 
Last edited:

pluton

Active Member
Licensed User
Longtime User
Maybe something like this:

B4X:
Dim out As OutputStream
   out = File.OpenOutput(File.DirDefaultExternal, Datafile, False)
   bitmap1.WriteToStream(out,100,"JPEG")
   out.Close
   ToastMessageShow(Datafile &".jpg saved to folder.",False)

That is something similar like your example just in mine I have bitmap1
 
Upvote 0

powerchen

Member
Licensed User
Longtime User
B4X:
Sub SaveFile(URL As String, LocalPath As String,Fname As String)
   If IsSuccess(URL) = False Then
      Log("Task not completed successfully.")
   End If
   File.Copy(HttpUtilsService.TempFolder, SuccessfulUrls.Get(URL),LocalPath,Fname)
End Sub

add to the HttpUtils.bas
 
Upvote 0

Andras

Active Member
Licensed User
Longtime User
Your code looks correct. Where is the code that loads the image?

Thanks, Erel and others.

The problem is certainly in some aspect of the downloading rather than the code to display the image; I know this because (i) if I mount the device as a disc drive and put the image into the folder that way, it displays properly and (ii) if I use ES File Manager to look into the Files folder after the picture has been downloaded with the code, it shows an error message against the picture, whereas no such error message is shown if the picture is put in by treating the device as a disc drive.

Odd, isn't it! I hesitate to suggest this, but is a bug being generated somewhere?

John
 
Upvote 0

Andras

Active Member
Licensed User
Longtime User
The picture is off our own company website, where I absolutely know it is and exactly what's there.

But I think I have the (an?) answer to the problem, and it lies in the image itself; there seems to be something non-standard in the encoding - Heaven knows what! - as other pictures work perfectly well. So yes, apparently not an HttpUtils problem directly, though it does seem to have been triggered by some bizarre element in the image.

Ah, the wonderful world of computers!

Thanks Erel - John
 
Upvote 0
Top