update widget not working

nico78

Active Member
Licensed User
Longtime User
Hello,

Here my code, why widget not update after the download finish?

B4X:
'Service module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim hc As HttpClient
   Dim target As OutputStream
   Dim rv As RemoteViews
   Dim image1 As Bitmap
   Dim out As OutputStream      
End Sub
Sub Service_Create
   ' Configure the homescreen widget. We let it update every 2 hours here. This is
   ' normally not necessary but I want to be sure that the clock restarts if there
   ' is any problem.

   rv = ConfigureHomeWidget("widget", "rv", 120, "image du jour")
   hc.Initialize("hc")
   
End Sub

Sub Service_Start (StartingIntent As Intent)
   ' If a widget is removed from homescreen we don't need to do anything
   If StartingIntent.Action = "android.appwidget.action.APPWIDGET_DELETED" Then Return
   
   ' Handle the widget events (RequestUpdate and Disabled)
   If rv.HandleWidgetEvents(StartingIntent) = False Then
      ' If the action is not handled by HandleWidgetEvents() then we
      ' probably were called by StartService() or StartServiceAt().
      ' So just update the widget.

         rv_RequestUpdate
      
   End If
   
   ' We have to be sure that we do not start the service
   ' again if all widgets are removed from homescreen
   If StartingIntent.Action <> "android.appwidget.action.APPWIDGET_DISABLED" Then
      StartServiceAt("", TimeToNextMinute, False)
   End If
End Sub

Sub rv_RequestUpdate
   ' Just update the time on the widget
   code.CallServer

End Sub

Sub rv_Disabled
   ' If all widgets are removed from homescreen cancel the next schedule and
   ' stop the service
   CancelScheduledService("")
   StopService("")
End Sub

Sub Service_Destroy

End Sub


Sub ImageView1_Click
   rv.SetImage( "imageview1",image1)
   
   rv.UpdateWidget 
End Sub 


Sub TimeToNextMinute As Long
   Dim ret As Long

   DateTime.TimeFormat = "HH:mm:ss"

   ret=DateTime.TimeParse("10:00:00")
   Log(DateTime.Time(ret))
   
   ret = (ret + DateTime.TicksPerDay)
   
   Log(DateTime.Time(ret))
   Log(DateTime.date(ret))
      
   Return ret
End Sub

Sub HC_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   Log("Success"&TaskId)

   Target = File.OpenOutput(File.DirInternalCache, "image.jpg", False)
   Response.GetAsynchronously("Response", Target, True, TaskId)

End Sub 

Sub HC_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
   Log("erreur")
   Log(reason)
   Log("ResponseError"&TaskId)

End Sub

Sub Response_StreamFinish (Success As Boolean, TaskId As Int)

      If Success = False Then
         ToastMessageShow("Error downloading file: " & LastException.Message, True)
         Log("StreamFinish erreur"&TaskId)
      Else
         Log("StreamFinish succes"&TaskId)
         
         Dim image As Bitmap
         If File.Exists(File.DirInternalCache, "image.jpg") Then
            image=LoadBitmap( File.DirInternalCache, "image.jpg")
            

            
            Dim canvas1 As Canvas
            
            
            Dim destrect As Rect
            Dim rapport As Float
            Dim largeur,hauteur As Int 
            
            If image.Height>image.Width Then
               rapport=image.Height/410dip
               hauteur=410dip
               largeur=image.Width/rapport
            Else
               rapport=image.Width/274dip
               largeur=274dip
               hauteur=(image.Height/rapport)
            End If 
               
            Log(largeur)
            Log(hauteur)
            
            image1.Initializemutable(largeur,hauteur)
            canvas1.Initialize2(image1)
            
            destrect.Initialize(0,0,largeur,hauteur)
            canvas1.DrawBitmap( image,Null,destrect)            
'            
            Log(image.Width)
            Log(image.Height)
            
'            out=File.OpenOutput( File.DirRootExternal,"image10.png",False)
'            Log( File.DirDefaultExternal)
'            canvas1.Bitmap.WriteToStream( out,100,"PNG")
'            out.close

            rv.SetImage( "imageview1",image1)
            rv.UpdateWidget                

         End If 
         
      End If
   
End Sub

B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub


Sub CallServer
   PostUrl = "http://www.b4x.com/android/images/logo2.png"
    Dim req As HttpRequest
    req.Initializeget( PostUrl)

    widget.hc.Execute(req, 0)

End Sub
 

nico78

Active Member
Licensed User
Longtime User
I can not explain it but now it works.
I do not understand!

Thanks for your help.
 
Upvote 0

nico78

Active Member
Licensed User
Longtime User
I believe to have understood the problem, it is not possible to load an image since the space private of the application.

Erel, can you confirm?


B4X:
Dim image2 As Bitmap
image2=LoadBitmap(File.DirInternal, "image.jpg") doesn't work
image2=LoadBitmap(File.RootExternal, "image.jpg") work
rv.SetImage( "ImageView1",image2)
rv.UpdateWidget
 
Upvote 0

nico78

Active Member
Licensed User
Longtime User
I really have a problem with rv.SetImage , it's doesn't work
error:
B4X:
java.lang.IllegalStateException: View com.android.internal.policy.impl.PhoneWindow$DecorView@40536bb0 has already been added to the window manager.

Can you help me?


B4X:
Sub Response_StreamFinish (Success As Boolean, TaskId As Int)

      If Success = False Then
         ToastMessageShow("Error downloading file: " & LastException.Message, True)
         Log("StreamFinish erreur"&TaskId)
      Else
      

         Log("StreamFinish succes"&TaskId)
         
         Log(File.DirInternal & "/" & fileimage)
         Log(File.DirRootExternal & "/copyimage."& extension)

         image1=LoadBitmap(File.DirInternal, fileimage)
         File.Copy(File.DirInternal, fileimage,File.DirRootExternal,"copyimage."& extension) ;for test if image exist
         rv.SetImage( "imageview1",image1)
         rv.UpdateWidget                
         
      End If
   
End Sub
 

Attachments

  • projet.zip
    8.3 KB · Views: 164
Upvote 0

nico78

Active Member
Licensed User
Longtime User
Whithout debugger, i have no error,

Can you see the code in the attached files
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
When you call SetImage, the image data is sent to the host process. Due to the size of these images the transaction fails. You can see a message about it in the unfiltered logs. The solution is simple.
Use LoadBitmapSample instead of LoadBitmap:
B4X:
image1=LoadBitmapSample(File.DirInternal, fileimage, 200dip, 200dip)
There is no reason to load the large bitmap as you are showing it much smaller.
 
Upvote 0

nico78

Active Member
Licensed User
Longtime User
yes, it's work with:
B4X:
image1=LoadBitmapSample(File.DirInternal, fileimage, 200dip, 200dip)

The dimension of the imageview is 280 * 280 and there that do not work, why?

B4X:
image1=LoadBitmapSample(File.DirInternal, fileimage, 280dip, 280dip)


It is curious, if I puts 260dip * 260dip, it does not work but if I make a rotation of the screen, the imageview updates and shows the image

Even with 200dip*200dip, it does not still work but with a rotation of the screen, yes.
 
Last edited:
Upvote 0

nico78

Active Member
Licensed User
Longtime User
It is annoying, I am obliged to load the image, re-size it in a canvas, save the image, reload it and make a rv.setimage
 
Upvote 0

JohnK

Active Member
Licensed User
Longtime User
I think its related, but if I use SetImage method with a bitmap simply loaded (from the asset directory, actually a sub directory), it fails to display, with no error. After I load the bitmap, the Height and width properties are returned correctly from the loaded bitmap.

However, if I paint the bitmap onto a canvas, and return the canvas bitmap and set the image to this bitmap, it works.

NB: This is all for a widget
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…