Android Question WebView LoadUrl Trying To Download

Juan Marrero

Active Member
Licensed User
Longtime User
Hi. I'm creating an app that connects to a main device and duplicate what the main device is doing (mirroring). I took the MJPEG_CCTV project and modified to send screenshots. In Windows via web browser when I connecto to the URL works perfectly. But when I use WebView is doing nothing. I event tried using the web browser in android and it's trying to download instead. What am I doing wrong? Any help is appreciated.

Main Module
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Private tmr As Timer
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.
   Private IntervalMs As Int = 100
   Private Button1 As Button
   Private Button2 As Button
   Private Button3 As Button
   Private Button4 As Button
   Private Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("Main")
   Log($"My ip: ${Starter.manager.MyIp}"$)
   tmr.Initialize("tmr", IntervalMs)
   tmr.Enabled = True
End Sub

Sub Activity_Resume
   UpdateUI
End Sub

Public Sub UpdateUI
   Log($"My ip: ${Starter.manager.MyIp} Number of clients: ${Starter.manager.NumberOfClients}"$)
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub TakeScreenshot As Bitmap
   Dim jo As JavaObject
   jo.InitializeContext
   Dim decor As JavaObject = jo.RunMethodJO("getWindow", Null).RunMethod("getDecorView", Null)
   Dim decorChild As JavaObject = decor.RunMethod("getChildAt", Array(0))
   decorChild.RunMethod("setDrawingCacheEnabled", Array(True))
   decorChild.RunMethod("buildDrawingCache", Null)
   Dim bmp As Bitmap = decorChild.RunMethod("getDrawingCache", Array(True))
   bmp.Initialize3(bmp)
   decorChild.RunMethod("setDrawingCacheEnabled", Array(False))
   Return bmp
End Sub

Sub XUIImageToJPEGByteArray(aImage As Object, width As Int, height As Int, quality As Int) As Byte()
   'Sanity check quality
   If (quality < -1 And quality > 100) Then
       Log("Parameter quality not in range (-1..100)")
       Return Array As Byte ()
   End If
   If quality = -1 Then quality = 75

   'Sanity check incoming image
   Try
       Dim image As B4XBitmap = aImage
   Catch
       Log(LastException)
       Return Array As Byte ()
   End Try
 
   'Sanity check incoming image dimensions
   Dim oldWidth As Int = image.Width
   Dim oldHeight As Int = image.Height
   If (oldWidth = 0 And oldHeight = 0 ) Then
       Log("Source image with incorrect dimensions")
       Return Array As Byte ()
   End If

   'See if were resizing the image
   Dim  resize As Boolean = True
   If ((width = 0 And height = 0) Or (width = -100 Or height = -100)) Then resize = False
 
   'Resize if necessary
   If (resize) Then
       Dim newWidth As Int = width
       Dim newHeight As Int = height
       'Calculate new dimensions
       If (newWidth < 0) Then newWidth = -1 * oldWidth * newWidth / 100
       If (newHeight < 0) Then newHeight = -1 * oldHeight * newHeight / 100
       If (newWidth = 0) Then newWidth = oldWidth * newHeight / oldHeight
       If (newHeight = 0 ) Then newHeight = oldHeight * newWidth / oldWidth
       image = image.Resize(newWidth, newHeight, False)
   End If
 
   'Convert image to JPEG byte array
   Dim out As OutputStream
   out.InitializeToBytesArray(0)
   image.WriteToStream(out, quality, "JPEG")
   out.Close
 
   'Done
   Return out.ToBytesArray
End Sub

Private Sub tmr_Tick
   Dim bm As Bitmap = TakeScreenshot
   If GetDeviceLayoutValues.Width > GetDeviceLayoutValues.Height Then
       Starter.manager.NewBitmap(XUIImageToJPEGByteArray(bm, 800, 600, 100))
   Else
       Starter.manager.NewBitmap(XUIImageToJPEGByteArray(bm, 600, 800, 100))
   End If
End Sub

Sub Button4_Click
   Label1.Text = "Button 4"
End Sub

Sub Button3_Click
   Label1.Text = "Button 3"
End Sub

Sub Button2_Click
   Label1.Text = "Button 2"
End Sub

Sub Button1_Click
   Label1.Text = "Button 1"
End Sub

Starter Module
B4X:
Sub Process_Globals
   Public manager As ServerManager
   Public EOL As String = Chr(13) & Chr(10)
End Sub

Sub Service_Create
   manager.Initialize(51042)
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_TaskRemoved
   'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
   Return True
End Sub

Sub Service_Destroy

End Sub

ServerManager and Client classes (.bas) are exactly the same as the MJPEG_CCTV project.
In the client app I'm just using WebView1.LoadUrl("http://.......").
 

Juan Marrero

Active Member
Licensed User
Longtime User
I tried the CCTV Client example in B4J and like 5% of the time the initial image is displayed but no refresh after that. 95% of the time just a blank screen.
 
Upvote 0
Top