Error Loading Bitmap, HttpUtils File.Copy2 testing

Stepdragon

Member
Licensed User
Longtime User
I'm working on figuring out how to async download basically anything I need for a program and isolate it into my own routines so i can copy and paste a snippet across future programs...

It was suggested that I use HttpUtils to download and then use File.Copy2 with HttpUtils.GetInputStream to save my file to a directory.

I think I have all that figured out, however when I'm trying to load my test image that I downloaded, I get the error 'Error loading Bitmap'.

I've tried switching between LoadBitmap and LoadBitmapSample. I've tried checking if the file exists, and I've even used a file explorer to load it into a different app.

I'm not sure what's going on here.

Here's my app code:
B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
   Dim URLwaiting As Map
   URLwaiting.Initialize

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim iv As ImageView
   iv.Initialize("")
   
   Dim btn As Button
   btn.Initialize("Button")
End Sub

Sub Activity_Create(FirstTime As Boolean)
   HttpUtils.CallbackActivity = "Main"
   HttpUtils.CallbackUrlDoneSub = "URLdone"
   
   CheckExtraURLs
   
   Activity.AddView(btn,30%x,10%y,40%x,20%y)
   Activity.AddView(iv,0,0,100%x,100%y)
   iv.SendToBack
   
   If File.Exists(File.DirRootExternal,"/pattern.jpg") Then
      'iv.Bitmap = LoadBitmap(File.DirRootExternal,"/pattern.jpg")
   End If
End Sub

Sub button_click
   If File.Exists(File.DirRootExternal,"/pattern.jpg") = False Then
      HttpUtils.Download("http://us.123rf.com/400wm/400/400/xiver/xiver1104/xiver110400035/9275412-television-test-screen.jpg","http://us.123rf.com/400wm/400/400/xiver/xiver1104/xiver110400035/9275412-television-test-screen.jpg")
      URLwaiting.Put("http://us.123rf.com/400wm/400/400/xiver/xiver1104/xiver110400035/9275412-television-test-screen.jpg","/pattern.jpg")
   Else
      Msgbox(File.DirRootExternal & "/pattern.jpg","")
      Msgbox(File.Exists(File.DirRootExternal , "/pattern.jpg"),"")
      iv.Bitmap = LoadBitmapSample(File.DirRootExternal,"/pattern.jpg",100%x,100%y)
   End If
   
End Sub

Sub UrlDone (URL As String)
   File.Copy2(HttpUtils.GetInputStream(URL),File.OpenOutput(File.DirRootExternal,URLwaiting.Get(URL),False))
   Msgbox(URLwaiting.Get(URL),"")
   For i = 0 To Activity.NumberOfViews - 1
      If Activity.GetView(i) Is ImageView Then
         Dim iv As ImageView
         iv.Initialize("")
         iv.Bitmap = LoadBitmapSample(File.DirRootExternal,"/pattern.jpg",100%x,100%y)
         Activity.GetView(i) = iv
      End If
   Next
End Sub

Sub CheckExtraURLs
   Dim inp As InputStream
   For i = 0 To URLwaiting.Size-1
      If inp = HttpUtils.GetInputStream(URLwaiting.GetKeyAt(i)) Then
         Log("Flie not yet downloaded")
      Else 
         File.Copy2(HttpUtils.GetInputStream(URLwaiting.GetKeyAt(i)),File.OpenOutput(File.DirRootExternal,URLwaiting.GetValueAt(i),False))
         URLwaiting.Remove(URLwaiting.GetKeyAt(i))
      End If
   Next
End Sub


Sub Activity_Resume
   CheckExtraURLs
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

This app has no additional libraries other than HttpUtils and HttpUtilsService.

Here's the Log from the error:

B4X:
java.lang.RuntimeException: Error loading bitmap.
   at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:495)
   at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize(CanvasWrapper.java:486)
   at anywheresoftware.b4a.keywords.Common.LoadBitmap(Common.java:911)
   at sdproductions.downloadtesting.main._activity_create(main.java:235)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:113)
   at sdproductions.downloadtesting.main.afterFirstLayout(main.java:84)
   at sdproductions.downloadtesting.main.access$100(main.java:16)
   at sdproductions.downloadtesting.main$WaitForLayout.run(main.java:72)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:130)
   at android.app.ActivityThread.main(ActivityThread.java:3703)


   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
   at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Error loading bitmap.

I do appreciate any help given.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The output stream is not closed automatically when you use File.Copy2 (to allow you to copy multiple streams).

You should change your code to:
B4X:
Dim out As OutputStream
out = File.OpenOutput(File.DirRootExternal,URLwaiting.GetValueAt(i),False)
File.Copy2(HttpUtils.GetInputStream(URLwaiting.GetKeyAt(i)),out)
out.close
 
Upvote 0

Stepdragon

Member
Licensed User
Longtime User
Thanks Erel, that worked perfectly.

I'm somewhat suprised I didn't see that... perhaps I overlooked it because of HttpUtils.GetInputStream auto closing the stream...

Thanks Again!
 
Upvote 0
Top