Android Question remoteViews.setUri ?

ducphu

Active Member
Licensed User
Longtime User
Hi all,

I have an imageview on my widget. Can I use "remoteViews.setUri" to set an image to my imageview?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
After implementing it, I found out that it only works with Android internal Uris.

B4X:
Sub SetUri(ClassName As String, ImageViewName As String, Uri As String)
   Dim xlb As XmlLayoutBuilder
   Dim id As Int = xlb.GetResourceId("id", ClassName.ToLowerCase & "_" & ImageViewName.ToLowerCase)
   Dim r As Reflector
   r.Target = rv
   r.RunMethod("checkNull")
   r.Target = r.GetField("current")
   Dim u As Object = r.RunStaticMethod("android.net.Uri", "parse", Array(Uri), Array As String("java.lang.String"))
   r.RunMethod4("setImageViewUri", Array(id, u), Array As String ("java.lang.int", "android.net.Uri"))
End Sub

Usage example:
B4X:
File.Copy(File.DirAssets, "smiley.png", File.DirRootExternal, "smiley.png")
   SetUri("WidgetService", "ImageView1", "file://" & File.Combine(File.DirRootExternal, "smiley.png"))
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime User

Thank Erel,

I saw in your code r.Target = rv, where should I declare rv?
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
Yeah, but how to declare it. I have undeclared variable when compiling. I tried
B4X:
Sub SetUri(rv as RemoveViews, ClassName As String, ImageViewName As String, Uri As String)

but no luck
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Yeah, but how to declare it
See the home widget tutorial and especially in WidgetService module
B4X:
'Service module
Sub Process_Globals
    Dim rv As RemoteViews
    Dim imageFiles() As String
    imageFiles = Array As String("player_button_next.png", "player_button_pause.png", _
        "player_button_play.png", "player_button_stop.png")
    Dim currentImage As Int
End Sub
Sub Service_Create
    'Set the widget to update every 60 minutes.
    rv = ConfigureHomeWidget("L1", "rv", 60, "Hello Widget")
End Sub
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
B4X:
Public Sub rv_RequestUpdate

    'This will generate a bitmap file called L_Header.png in File.DirInternal
    CommonCode.Generate_Image("L","Header",header,0,0,False)
   
    'This command works: rv.SetImage("Header",LoadBitmapSample(File.DirInternal,"L_Header.png",125dip,25dip))
    SetUri("widget2x2L", "Header", "file://" & File.Combine(File.DirInternal, "L_Header.png")) 'widget2x2L is my service's name

    rv.UpdateWidget
End Sub

Sub SetUri(ClassName As String, ImageViewName As String, Uri As String)
   Dim xlb As XmlLayoutBuilder
   Dim id As Int = xlb.GetResourceId("id", ClassName.ToLowerCase & "_" & ImageViewName.ToLowerCase)
   Dim r As Reflector
   
   r.Target = rv
   r.RunMethod("checkNull")
   r.Target = r.GetField("current")
   Dim u As Object = r.RunStaticMethod("android.net.Uri", "parse", Array(Uri), Array As String("java.lang.String"))
   r.RunMethod4("setImageViewUri", Array(id, u), Array As String ("java.lang.int", "android.net.Uri"))
End Sub


I tried this but it doesn't work
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
Hi Erel,

I encouter an issue with SetUri.
B4X:
Public Sub rv_RequestUpdate

    'This will generate a bitmap file called L_Header.png in File.DirInternal
    CommonCode.Generate_Image("L","Header",header,0,0,False)
  
    'This command works: rv.SetImage("Header",LoadBitmapSample(File.DirInternal,"L_Header.png",125dip,25dip))
    SetUri("widget2x2L", "Header", "file://" & File.Combine(File.DirInternal, "L_Header.png")) 'widget2x2L is my service's name

    rv.UpdateWidget
End Sub

When I add a new widget, this code works fine. But when I try to update the existing widget, e.g. StartServiceAt the imageview doesn't update to the new bitmap. I checked and confirmed that the new bitmap is generated.
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
Huh,

but if I put a toastmessage, it shows up. Also, if I use rv.setimage instead of SetUri, it works.
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
B4X:
Sub Service_Create
    rv = ConfigureHomeWidget("widget2x2", "rv", 1440, "E-Calendar(Lunar)")
   
    
    If File.Exists(File.DirDefaultExternal, "L_Header.png") Then
        rv.SetImage("Header",LoadBitmapSample(File.DirDefaultExternal,"L_Header.png",125dip,25dip))
        'SetUri("widget2x2L", "Header", "file://" & File.Combine(File.DirDefaultExternal, "L_Header.png"))
    End If
    rv.UpdateWidget
End Sub

B4X:
Sub rv_RequestUpdate

    'This will generate a bitmap file called L_Header.png in File.DirInternal
    CommonCode.Generate_Image("L","Header",header,0,0,False)
 
    rv.SetImage("Header",LoadBitmapSample(File.DirInternal,"L_Header.png",125dip,25dip))
    'SetUri("widget2x2L", "Header", "file://" & File.Combine(File.DirInternal, "L_Header.png")) 'widget2x2L is my service's name

    rv.UpdateWidget
End Sub

When I call startservice, rv.setimage updates existing widget while SetUri doesn't.
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
Ok, I manage to find a way to fix this. Basically I set the uri to a blank.png then change it back to the bitmap I want. I realize that if the path and file name are the same, SetUri simply ignore it so the bitmap will not get updated. Here is the solution:

B4X:
Sub Service_Create
    rv = ConfigureHomeWidget("widget2x2", "rv", 1440, "E-Calendar(Lunar)")
  
    If File.Exists(File.DirDefaultExternal, "L_Header.png") Then SetUri("widget2x2L", "Header", "file://" & File.Combine(File.DirDefaultExternal, "L_Header.png"))
    rv.UpdateWidget
End Sub

Sub rv_RequestUpdate

    ' Set the uri to a blank png
    SetUri("widget2x2L", "Header", "file://" & File.Combine(File.DirDefaultExternal, "blank.png"))
    rv.UpdateWidget

    'This will generate a bitmap file called L_Header.png in File.DirInternal
    CommonCode.Generate_Image("L","Header",header,0,0,False)

    SetUri("widget2x2L", "Header", "file://" & File.Combine(File.DirDefaultExternal, "L_Header.png"))
    rv.UpdateWidget

End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…