Android Question Please Help - Download Image From Web

RodrigoVitachi

Member
Licensed User
I tried it really hard for days, with no success. Please tell me how to download images from web using HttpJob ( HttpUtils2 )

The final result that i am looking for is to download an image from my website to disk ( File.DirRootExternal ).

Code:

B4X:
Dim jobPost As HttpJob
jobPost.Initialize("Imagem",Me)
jobPost.Download(imageURL) 'i am using http://techsim.com/ads/img/1PnHmtg5Qy4JU70vwNOJ.jpg
B4X:
Sub JobDone (Job As HttpJob)
  If Job.Success = True Then
     If Job.JobName="Imagem" Then
       Dim img As Bitmap = Job.GetBitmap <---- ERROR
     End If
  End If
End Sub
Error i get:

** Service (httputils2service) Create **
** Service (httputils2service) Start **
Error occurred on line: 56 (funcoes)
java.io.FileNotFoundException: /data/data/b4a.example/cache: open failed: EISDIR (Is a directory)
at libcore.io.IoBridge.open(IoBridge.java:456)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:209)
at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize(CanvasWrapper.java:498)
at anywheresoftware.b4a.keywords.Common.LoadBitmap(Common.java:1111)
at anywheresoftware.b4a.samples.httputils2.httpjob._getbitmap(httpjob.java:116)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:633)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:298)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:237)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:116)
at anywheresoftware.b4a.BA$3.run(BA.java:319)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.system.ErrnoException: open failed: EISDIR (Is a directory)
at libcore.io.IoBridge.open(IoBridge.java:446)
... 22 more

Ps.: It doesn't matter what i use to catch "GetBitmap", it always will return the same error.
I already tried to delete cache files, remove everything from my celphone linked to the app, etc. No results.

If i just call Job.GetBitmap i will have the same result, wich means there is something wrong in the method itself, calling something weird in the "framework".

Please, any help :)
 
Last edited:

DonManfred

Expert
Licensed User
put the filename in Job.tag before calling download
then in Jobdone
B4X:
      Log("DownloadReady: "&job.Tag)
      OutStream = File.OpenOutput(File.DirRootExternal, job.Tag, False) ' Job.Tag is read to set the Original Filename we specify earlier in the creation of the Job
      File.Copy2(job.GetInputStream,OutStream) ' save the file
      OutStream.Close
      Log(job.Tag&" written to "&File.DirRootExternal) ' Write the Originalname to the log to see what happens ;-)
 

RodrigoVitachi

Member
Licensed User
Hey DonManfred, thanks for your reply :)

File.Copy2(job.GetInputStream,OutStream) is giving me exactly the same error, probably the GetInputstream method.
 

RodrigoVitachi

Member
Licensed User
Jeez, just found the problem and solution.

For anyone with the same problem, it is because i was using the same HttpJob for a previous post, to retrieve the image URL.
If i reuse the service, it will break, then i just created another HttpJob variable and used it only to download the image, and now its working fine :)

Thanks DonManfred for your "download to file" code, i didn't know how to do that after starting the download service :D
 
Top