Android Question Load pictures from parse server in a scroll feed

hookshy

Well-Known Member
Licensed User
Longtime User
I try to get parse files saved on server as jpeg images
when I get the information back from server I receive byte of data as shown below

B4X:
Sub pf_DoneGet (Success As Boolean, Data() As Byte, TaskID As Int)

  Dim pf As ParseFile
    pf = Sender
  Dim msg As String
  msg = "Getting file: " & pf.Name &  " Success=" & Success
  Log(msg)
 
  ToastMessageShow(msg, False)



  If Success = False Then
     Log(LastException.Message)
    Else
        msg = BytesToString(Data, 0, Data.Length, "UTF-8")
        Log(msg)
  End If


End Sub


Question :
How do I use the data byte to display the jpg image to a scroll view ?
I imagine that data() as byte can be converted back to image but how ?
 

hookshy

Well-Known Member
Licensed User
Longtime User
I found this code and it worked

B4X:
   Dim In As InputStream
   In.InitializeFromBytesArray(Data, 0, Data.Length)
   Dim bmp As Bitmap
   bmp.Initialize2(In)
   In.Close
 
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
Any ideas how to load 1000 pictures to a feed scrollview ?
I see that images are loaded just when you scroll on the feed lines ... How news feed work ?
for now it seems my attempt to load pictures from a data server failed ....
 
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
A bit closer but still have some questions because my code seems to crash the app from time to time
What I did:
1. stored the parse files in a map when I list the server data and fill in the customscrolview with a blank icon
B4X:
Dim pf As ParseFile
pf = po.GetObject("picture")
Dim detail As String
detail=po.GetString("detail")
Dim objectId As String
objectId=po.objectId' each of my pannels from customscrollview have the same tag as the objectid from parse query
idmap.Put(i,objectId)'unde i este taskid        
picmap.Put(objectId,pf)

2. then using scroll position I track the panels that are shown to the user and send a request to parse to retrieve the information behind the parse objects
B4X:
Private Sub sv_ScrollChanged(Position As Int)
If DateTime.Now > lastAddItemsTime + 100 Then
       lastAddItemsTime = DateTime.Now
       CallSubDelayed2(Main, "scrol1_additems",Position)  
     End If
end sub

3. as the information is ready i use ImputScreen to convert the data to bitmap


Is the imputstream a good metod to retreive jpg pictures drom byte array ? do I need some other libraries ?
How can I load just a sample of my picture as long as I am going to load it to a small image icon ? I guess this is about saveing resources


if I try to use the imputstream more often like like asking parse to retreive more pictures at once the app crashes
tests crashed the app with only 5 pictures or so .... the problem can be with parse library but I can not track the errors


B4X:
Sub pftest_DoneGet (Success As Boolean, Data() As Byte, TaskID As Int)

  Dim pf As ParseFile
  pf = Sender


   Dim In As InputStream
   In.InitializeFromBytesArray(Data, 0, Data.Length)
   Dim bmp As Bitmap
   bmp.Initialize2(In)
   In.Close


Dim pnlx As Panel
pnlx=scrol1.GetPanel(TaskID) 'task id is the view position in custom scrollview
Dim img As ImageView
img=pnlx.GetView(0)
img.SetBackgroundImage(bmp)


  If Success = False Then
     Log(LastException.Message)
  End If


End Sub
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Is the imputstream a good metod to retreive jpg pictures drom byte array ? do I need some other libraries ?
How can I load just a sample of my picture as long as I am going to load it to a small image icon ? I guess this is about saveing resources
Are you using HttpUtils2 to download the images?
 
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
Are you using HttpUtils2 to download the images?
No , parse library from parse.com is resposible for downloading the images ...
I see that if the images are 300k the feed is working good but when the pictures is greater than 1M then I meet crashes from time to time
in the feed I load only an icon 120x120 dip and maybe the imputstream loads original bitmap .

It might be related to parse issue ...I will search their forums as well

here is the code from parse query to retreive parse files
pf.get action raise pf_doneget event I retrieve parse files data when the user scrolls the customlistview
B4X:
Sub refresh_feed_click

  Dim query As ParseQuery
  query.Initialize("feed")
  query.Find("get_feed", 1)
ToastMessageShow("Get feed",True)

End Sub

Sub get_feed_DoneFind (Success As Boolean, ListOfPO As List, TaskId As Int)


  If Success = False Then
      Log("Error: " & LastException.Message)
      ToastMessageShow("Error: " & LastException.Message, True)
  
  Else
 
      Dim sb As StringBuilder
      sb.Initialize
      For i = 0 To ListOfPO.Size - 1
      Try
  
  
          Dim po As ParseObject
          po = ListOfPO.Get(i)
                  

                        Dim pf As ParseFile
                        pf = po.GetObject("picture")
                   

                    Dim detail As String
                    detail=po.GetString("detail")
              
                    Dim objectId As String
                    objectId=po.objectId
picmap.Put(objectId,pf)
                

                    afisare_feed(sos,detail,objectId)
                    'add a feed element with a blank picture
                
    
                    Catch
                    ToastMessageShow("error",True)
                    End Try
            
  
      Next
  
  
  End If
End Sub
 
Last edited:
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
Here is the story
Scroll change event is asking to retrieve the pictures with the id of the views that are on front (code on the bottom)
The bitmap is looked up in cache storage , if exists then it is loaded to the imageview
If the image is not in cache , the a parse get file request is fired
When the file is ready then it is saved to cache memory

It does work but things are not as simple as it looks , Out of memory crashes the app , large heap is useless right now
Every time I scroll the views it seems that the memory is going down , even if the bitmap is found and loaded from cache ...and I do not understand why ?

is there an example that load a bunch of images in a scroll view from internet ?
I have tried Google play store ..seems to work nice


I might misunderstood the cache functionality ...I do not find any resources right now to get the things working somehow

Before working with the work of another library like ULV from Informatics I want to understand how loading images works , and what shall stand behind the
news feed loading .

B4X:
Sub scrol1_refresh_data(position As Int)

     Dim percentofcache As Float
    percentofcache=(Cache.FreeMemory/maxcache)*100
    percentofcache=NumberFormat(percentofcache,0,0)
    lbl1total.Text=percentofcache'Cache.FreeMemory&" / "&Cache.MaxMemory
Dim scrol_size,panel_height As Int
scrol_size=scrol1.GetSize 'number of views

'read the height of the panel
Dim pnl As Panel
pnl=scrol1.GetPanel(0)
panel_height=pnl.Height



Dim start,stop As Int
start = Round2(position/panel_height,0)
start=Max(0,start)'set the limits for panel 0 and max panel index

stop= Round2(scrol1.AsView.Height/panel_height,0)+start
stop=Min(stop,scrol1.GetSize-1) 'set the limits for panel 0 and max panel index


For i=start To stop Step 1

Dim pnlx As Panel
pnlx=scrol1.GetPanel(i)

    ' The memory Cache Is looked first
    Dim bmp As Bitmap
    bmp = Cache.GetBitmap(i, True)  

  
    If bmp.IsInitialized Then
        refresh_image(bmp ,pnlx,"CACHE")
    Else ' get image from parse file

        Dim pfx As ParseFile
        pfx=picmap.Get(pnlx.Tag)' picmap the map previously stored from parse query
        pfx.Get("pf",i)
        'pending refresh image
  
    End If
  
Next
End Sub
Sub pf_DoneGet (Success As Boolean, Data() As Byte, TaskID As Int)

  Dim pf As ParseFile
    pf = Sender

   Dim In As InputStream
   In.InitializeFromBytesArray(Data, 0, Data.Length)
   Dim bmp As Bitmap
   bmp.Initialize2(In)
   In.Close

Dim pnlx As Panel
pnlx=scrol1.GetPanel(TaskID)


    'scales the bitmap
    Dim sd As Rect
    sd.Initialize(0,0,100dip,100dip)
    Dim bmpx As BitmapPlus
    bmp=bmpx.createScaledBitmap(bmp,100dip,100dip,False)
  
    'put the image to cache
    Cache.PutBitmapInMemory(TaskID, bmp)
    Cache.PutBitmapOnDisk_Async(TaskID, bmp, "PNG", 90, "Cache")


    refresh_image(bmp,pnlx,"PARSE")

End Sub
Sub refresh_image(bmp As Bitmap,pnlx As Panel,NAME As String)

Log(NAME)'the picture was loaded from cache or parse server

Dim img As ImageView
img=pnlx.GetView(0)
img.Gravity=Gravity.FILL
img.SetBackgroundImage(bmp)

End Sub

Sub Cache_PutDone(Key As String, Error As Boolean)
    ' This "PutDone" event is triggered after the bitmap has been put in the disk cache
    If Error Then
        Log("Error (" & Key & "): " & LastException.Message)
        ToastMessageShow("Error (" & Key & "): " & LastException.Message, True)
    Else
        Log("Bitmap put in cache: " & Key)
    End If
End Sub

B4X:
Private Sub sv_ScrollChanged(Position As Int)

Dim pnlindex As Float
pnlindex=Round2(Position/Main.pnlheight,0)

If pnlindex <> Main.lastindex  AND DateTime.Now > lastAddItemsTime + 100 Then
    Main.lastindex=pnlindex
    lastAddItemsTime=DateTime.now
    CallSubDelayed2(Main, "scrol1_refresh_data",Position)
End If

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