Android Question How to load image from api to image control

junaidahmed

Well-Known Member
Licensed User
Longtime User
I have an rest api get method which return Image URL.I would like to know how to set this Image URL in Image control .i have tried below code but its shows an error message "java.lang.classCasstException: can not be cast to android.graphics.bitmap"

Public Sub GetImage(URL As String, Parameters() As String, Token As String) As ResumableSub

Dim Result As String
Dim j As HttpJob
Try
j.Initialize("", Me)
j.Download2(Main.Link & URL, Parameters)
'j.GetRequest.SetContentType("application/json")
'j.GetRequest.SetHeader("Content-Length",Parameters.Length)
If Not(Token.Length = 0) Then
j.GetRequest.SetHeader("Authorization", $"Bearer ${Token}"$)
End If
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Result = j.GetString
JSON.Initialize(Result) 'Read the text from a file.
Map1 = JSON.NextObject
Log(Map1.Get("ImageURL"))
ImgEmp.SetBackgroundImage(Map1.Get("ImageURL"))

Else
'xui.MsgboxAsync( j.Response.ErrorResponse,"")
Result = j.Response.ErrorResponse
End If
Catch
Log(LastException)
xui.MsgboxAsync(LastException,"exception")
End Try
j.Release
Return Result
End Sub
 

drgottjr

Expert
Licensed User
Longtime User
Log(Map1.Get("ImageURL"))
1) this is not an image (or bitmap). if is, presumably, the url where you will find it. you need to go back and download the imabe from its url. just go back with okhttputils2.
2) in the extremely unlikely event, "ImageURL" is a bitmap, it is likely to be base64, so you would need to convert it to a byte array and then to a bitmap. show us the json string (Result = j.GetString)
 
Upvote 0

junaidahmed

Well-Known Member
Licensed User
Longtime User
1) this is not an image (or bitmap). if is, presumably, the url where you will find it. you need to go back and download the imabe from its url. just go back with okhttputils2.
2) in the extremely unlikely event, "ImageURL" is a bitmap, it is likely to be base64, so you would need to convert it to a byte array and then to a bitmap. show us the json string (Result = j.GetString)

Can you provide example code to convert image url to byte array in b4A ??
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
?
"Data:Image/PNG;Base64,Ivborw0kggoaaaansuheugaabvyaaamacaiaaabaxku...........
B4X:
'    get data to string
    Dim strImage As String = Map1.Get("ImageURL")
   
'    Delete head data and get base64
    strImage = strImage.Replace("Data:Image/PNG;Base64,", "")
   
'    Convert to image
    Dim bmpImage As B4XBitmap = BytesToImage(SU.DecodeBase64(strImage))
   
'    set image to view
    B4XImageView1.SetBitmap(bmpImage)
B4X:
Public Sub BytesToImage(bytes() As Byte) As B4XBitmap
    Dim In As InputStream
    In.InitializeFromBytesArray(bytes, 0, bytes.Length)
    #if B4A or B4i
    Dim bmp As Bitmap
    bmp.Initialize2(In)
    #else
    Dim bmp As Image
    bmp.Initialize2(In)
    #end if
    Return bmp
End Sub
 
Upvote 0

junaidahmed

Well-Known Member
Licensed User
Longtime User
?

B4X:
'    get data to string
    Dim strImage As String = Map1.Get("ImageURL")
   
'    Delete head data and get base64
    strImage = strImage.Replace("Data:Image/PNG;Base64,", "")
   
'    Convert to image
    Dim bmpImage As B4XBitmap = BytesToImage(SU.DecodeBase64(strImage))
   
'    set image to view
    B4XImageView1.SetBitmap(bmpImage)
B4X:
Public Sub BytesToImage(bytes() As Byte) As B4XBitmap
    Dim In As InputStream
    In.InitializeFromBytesArray(bytes, 0, bytes.Length)
    #if B4A or B4i
    Dim bmp As Bitmap
    bmp.Initialize2(In)
    #else
    Dim bmp As Image
    bmp.Initialize2(In)
    #end if
    Return bmp
End Sub
sorry for late reply....
I would like to load image (which is not base64 from api,its only string path for example www.abcd.4634.jpg ) in imageview.
i have tried above code but its showing as "Bad Base64" input
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
?
See please

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
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
or you could use SimpleMediaManager to perform the download and update a placeholder panel..

 
Upvote 0
Top