Android Tutorial [B4X] [B4XPages] Flickr Stream - Cross platform stream of photos

Erel

Administrator
Staff member
Licensed User

Example of showing online images, which are downloaded from flickr as the user scrolls the list.
The list is smooth and works very nice (in release mode).

This example demonstrates several interesting things and I'm pretty sure that most developers will learn a trick or two from the code.
  1. It is cross platform and all the important code is shared between the three projects (https://www.b4x.com/android/forum/threads/114125/#content).
  2. The program is made of two classes:
    • FlickrFeedReader - This class downloads the meta information - FlickrPhotos, and downloads the images when needed. It manages the images cache.
      It doesn't show any UI. In B4A, where the UI is recreated together with the activity, the reader is a process global variable that is initialized in the starter service.
      It uses a timer that checks the current list index every 100ms and then tests whether the relevant images are available. If not, it downloads the images.
    • FlickrFeedUI - This class manages the CLV list. In this example the items are only created when the user scrolls the list. This can be useful in all kinds of cases, such as implementations of endless lists. In this case there is a limit of 500 items related to the meta information.
      In B4A, the FlickFeedUI is a Global object that is created when the activity is created.
  3. Resizing bitmaps is a heavy operation, which is done on the CPU. Instead of resizing the bitmaps we resize the ImageViews to make the image fill the CLV item. This way the operation is hardware accelerated.
  4. The CLV implements the good old lazy loading method based on VisibleRangeChanged.
  5. The images are cached with B4XOrderedMap from B4XCollections.
    When an image is used, we check its current index and move it to the back of the list if needed. Later we will remove "old" images started from the beginning.
    B4X:
    If DownloadedPhotos.ContainsKey(item.Id) Then
    Dim index As Int = DownloadedPhotos.Keys.IndexOf(item.Id)
    If index < DownloadedPhotos.Size - 10 Then
    'move it to the back of the list
    DownloadedPhotos.Keys.RemoveAt(index)
    DownloadedPhotos.Keys.Add(item.Id)
    End If
    Else If CurrentlyDownloadingIds.Contains(item.Id) = False Then
    DownloadPhoto (item)
    End If
  6. AnotherProgressBar is displayed whenever there are visible items waiting for images.

Note that the feed might include images not appropriate for kids.
 

Attachments

Last edited:

Johan Hormaza

Active Member
Licensed User
Hi.
I have this error: ResponseError. Reason: javax.net.ssl.SSLHandshakeException: Connection closed by peer, Response:
 

Johan Hormaza

Active Member
Licensed User
Does it crash? Which platform and which link fails?
Are you sure that the device is connected to the internet?
On all platforms, and if they are all connected to the internet.
in B4J

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
ResponseError. Reason: javax.net.ssl.SSLException: SSL peer shut down incorrectly, Response:
javax.net.ssl.SSLException: SSL peer shut down incorrectly
at sun.security.ssl.InputRecord.readV3Record(InputRecord.java:596)
at sun.security.ssl.InputRecord.read(InputRecord.java:532)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:975)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1367)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1395)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1379)
at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:242)
at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:200)
at okhttp3.internal.connection.RealConnection.buildConnection(RealConnection.java:174)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:114)
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:196)
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:132)
at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:101)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:179)
at okhttp3.RealCall.execute(RealCall.java:63)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.executeWithTimeout(OkHttpClientWrapper.java:156)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.access$0(OkHttpClientWrapper.java:153)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$ExecuteHelper.run(OkHttpClientWrapper.java:201)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
ResponseError. Reason: javax.net.ssl.SSLException: SSL peer shut down incorrectly, Response:
javax.net.ssl.SSLException: SSL peer shut down incorrectly
 
Top