Android Question Transfer image from one B4XView to another

sdixon

Member
Licensed User
Longtime User
i download an image and place it into a B4XView.

I would also like to use the same image in another B4XView.

How can I transfer the image so I don't have to download it twice?

Thanks
 

mangojack

Expert
Licensed User
Longtime User
B4X:
ImageView2.SetBitmap(ImageView1.GetBitmap)
 
Upvote 0

sdixon

Member
Licensed User
Longtime User
B4X:
ImageView2.SetBitmap(ImageView1.GetBitmap)

this is the error I get but
ImageView2 has already been initialized as a B4XView


main_createitem (java line: 910)
java.lang.RuntimeException: Object should first be initialized (B4XBitmap).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67)
at com.unicorn.bali_tourist.main._createitem(main.java:910)
at com.unicorn.bali_tourist.main._loadpremiumlistings(main.java:1522)
at com.unicorn.bali_tourist.main$ResumableSub_Activity_Create.resume(main.java:645)
at com.unicorn.bali_tourist.main._activity_create(main.java:376)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at com.unicorn.bali_tourist.main.afterFirstLayout(main.java:105)
at com.unicorn.bali_tourist.main.access$000(main.java:17)
at com.unicorn.bali_tourist.main$WaitForLayout.run(main.java:83)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
I don't think the error is about the second ImageView ... more the bitmap you are trying to copy.

How are you declaring the downloaded Image ? As a B4XBitmap?
 
Upvote 0

sdixon

Member
Licensed User
Longtime User
B4X:
DownloadImage(ItemImage, imgItem)

ItemImage = https://image...
imgItem is declsred as a B4XView

B4X:
Sub DownloadImage(Link As String, iv As ImageView)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        iv.Bitmap = j.GetBitmap
    End If
    j.Release
End Sub

This works fine and the B4XView is filled with the image I am expecting

When I try to transfer the image I get the error message
 
Upvote 0

Biswajit

Active Member
Licensed User
Longtime User
B4X:
Sub DownloadImage(Link As String, iv As ImageView)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        iv.Bitmap = j.GetBitmap
        iv.tag = j.GetBitmap '<- store the image in imageview tag
    End If
    j.Release
End Sub

iv2.bitmap = iv.tag '<- then set the image to 2nd imageview
 
Upvote 0

sdixon

Member
Licensed User
Longtime User
B4X:
Sub DownloadImage(Link As String, iv As ImageView)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        iv.Bitmap = j.GetBitmap
        iv.tag = j.GetBitmap '<- store the image in imageview tag
    End If
    j.Release
End Sub

iv2.bitmap = iv.tag '<- then set the image to 2nd imageview

Thanks for your help

I changed the sub as follows

B4X:
sub DownloadListingImage(Link As String, iv As ImageView, iv1 As ImageView)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        iv.Bitmap = j.GetBitmap
        iv1.Bitmap = j.GetBitmap
    End If
    j.Release
End Sub

that works but my original question -
Transfer image from one B4XView to another
is still a mystery. Maybe next time.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
B4X:
B4XView1.SetBitmap(B4XView2.Bitmap)
'or

B4XView1.SetBitmap(B4XView2.SnapShot)
 
Upvote 0

Biswajit

Active Member
Licensed User
Longtime User
that works but my original question -
Transfer image from one B4XView to another
is still a mystery. Maybe next time.
I have tested this code and its is working fine
B4X:
ImageView2.SetBitmap(ImageView1.GetBitmap)

Maybe you are setting the 2nd imageview before the download is finished.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
B4X:
B4XView1.SetBitmap(B4XView2.Bitmap)
'or

B4XView1.SetBitmap(B4XView2.SnapShot)

this should work but i assume you wont get real bitmap with its original resolution.
if you put a 4k bitmap on a 32pixel view you will get like this 32pixel bitmap.

i agree that such function is missing in xui.
what i would try to do is create an imageview and set it to the b4xview.
the imageview has the method .Bitmap

that should work but this is no more b4x


B4X:
    Dim img As ImageView = b4xview
    b4xview.SetBitmap(img.Bitmap)
 
Last edited:
Upvote 0

sdixon

Member
Licensed User
Longtime User
i agree that such function is missing in xui.
what i would try to do is create an imageview and set it to the b4xview.
the imageview has the method .GetBitmap

that should work but this is no more b4x

I guess we'll have to wait - what I want has been accomplished by my workaround. Thanks for all your help
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Ilan's answer is the correct answer.

The most important feature of B4XView, is that you are never limited to the methods exposed by B4XView. You can always cast the view to its native type.

B4X:
Sub GetBitmap (ImageView As B4XView) As B4XView
 Dim iv As ImageView = ImageView
 #if B4J
    Return iv.GetImage
 #else
   Return iv.Bitmap
 #end if
End Sub
 
Upvote 0
Top