B4A Library Picasso image downloading and caching library

Picasso is A powerful image downloading and caching library for Android.

The existing b4a ImageDownloader module does much the same as Picasso but Picasso's main advantage that it can perform various image transformations for you.

Picasso consists of three object: Picasso, RequestBuilder and DefaultTarget:

Picasso
Comment:
Picasso is an open source library that manages the loading of images in your application.
http://square.github.io/picasso/
Licensed under the Apache License, version 2.0:
http://www.apache.org/licenses/LICENSE-2.0
Author: Martin Pearman
Version: 1
  • DefaultTarget
    Events:
    • Error (Tag As Object)
    • Success (Bitmap1 As Bitmap, Tag As Object)
    Methods:
    • Initialize (EventName As String, Tag As Object)
    • IsInitialized As Boolean
  • Picasso
    Methods:
    • CancelRequest (ImageView1 As ImageView)
      Cancel any existing requests for the specified target ImageView1.
    • CancelRequest2 (Target1 As Target)
      Cancel any existing requests for the specified Target instance.
    • Initialize
    • IsDebugging As Boolean
      Returns True if debug display, logging, and statistics are enabled.
    • IsInitialized As Boolean
    • LoadFile (FilePath As String) As RequestBuilder
      Start an image request using the specified image file path.
    • LoadResource (ResourceName As String) As RequestBuilder
      Start an image request using the specified drawable resource.
    • LoadUrl (Url As String) As RequestBuilder
      Start an image request using the specified URL.
    • SetDebugging (Debugging As Boolean)
      Set whether debug display, logging, and statistics are enabled.
    Permissions:
    • android.permission.INTERNET
  • RequestBuilder
    Methods:
    • CenterCrop As RequestBuilder
      Crops an image inside of the bounds specified by Resize(TargetWidth, TargetHeight) rather than distorting the aspect ratio.
      CenterCrop can only be used after calling Resize.
    • CenterInside As RequestBuilder
      CenterInside can only be used after calling Resize.
    • ErrorDrawable (ErrorDrawable As Drawable) As RequestBuilder
      Set a Drawable to be used if the requested image could not be loaded.
    • ErrorResource (ResourceName As String) As RequestBuilder
      Set a drawable resource to be used if the requested image could not be loaded.
    • Fetch (Target1 As Target)
      Asynchronously fulfils the request into the specified Target1.
    • Fit As RequestBuilder
      Attempt to resize the image to fit exactly into the target ImageView's bounds.
    • Get As BitmapWrapper
      Synchronously fulfill this request.
    • IntoImageView (ImageView1 As ImageView)
      Asynchronously fulfils the request into the specified ImageView.
    • IntoTarget (Target1 As Target)
      Asynchronously fulfils the request into the specified Target.
      See also the RequestBuilder Fetch method.
    • IsInitialized As Boolean
    • NoFade As RequestBuilder
      Disable brief fade in of images loaded from the disk cache or network.
    • PlaceholderDrawable (PlaceholderDrawable As Drawable) As RequestBuilder
      Set a Drawable to be used while the requested image is being loaded.
    • PlaceholderResource (ResourceName As String) As RequestBuilder
      Set a drawable resource to be used while the requested image is being loaded.
    • Resize (TargetWidth As Int, TargetHeight As Int) As RequestBuilder
      Resize the image to the specified size in pixels.
    • ResizeDimen (TargetWidthResourceName As String, TargetHeightResourceName As String) As RequestBuilder
      Resize the image to the specified resource dimensions size.
    • Rotate (Degrees As Float) As RequestBuilder
      Rotate the image by the specified degrees.
    • Rotate2 (Degrees As Float, PivotX As Float, PivotY As Float) As RequestBuilder
      Rotate the image by the specified degrees around the specified pivot point.
    • Scale (Factor As Float) As RequestBuilder
      Scale the image using the specified factor.
    • Scale2 (FactorX As Float, FactorY As Float) As RequestBuilder
      Scale the image using the specified factors.
    • SkipCache As RequestBuilder
      Indicate that this request should not use the memory cache for attempting to load or save the image.
    • Transform (Transformation1 As Transformation) As RequestBuilder
      Add a custom transformation to be applied to the image.
      ** The Transformation interface is not currently implemented so this method has no use **


As a simple image downloader you can use Picasso to download an image and then set the downloaded image as an ImageView Bitmap:

B4X:
Picasso1.LoadUrl("http://i.imgur.com/DvpvklR.png").IntoImageView(ImageView1)

The DefaultTarget object offers an alternative syntax.
Instead of Picasso setting the downloaded image as an ImageView Bitmap it can raise an event and pass the downloaded Bitmap to a b4a Sub:

B4X:
Dim Target1 As DefaultTarget
Target1.Initialize("Target1", "MyTagValue")
Picasso1.LoadUrl("http://i.imgur.com/DvpvklR.png").Fetch(Target1)

Sub Target1_Error(Tag As Object)
   Log("Target1_Error Tag="&Tag)
End Sub

Sub Target1_Success(Bitmap1 As Bitmap, Tag As Object)
   Log("Target1_Success Tag="&Tag)
   If Tag="MyTagValue" Then
     '   do something with the Bitmap
   End If
End Sub

So using the DefaultTarget allows you to do whatever you want to do with the downloaded image.

Picasso manages downloaded images with an in-memory cache and a disk cache, it'll retrieve an image from the caches if possible before trying to download the image.
When Picasso does download an image from the internet it looks at the various HTTP cache instruction headers that accompany the image.
Picasso then decides how long to cache the downloaded image to memory or disk based on these HTTP headers.

Another useful feature of Picasso is it's ability to display 'image downloading' and 'image download failed' placeholder images.

I've created two example projects.
One shows the use of loading an image into an ImageView with the various transformation and placeholder options.
The other is a simple example showing how to use the DefaultTarget.

Library and example projects are attached.
Please read the readme.txt file in the library download.

Martin.
 

Attachments

  • Picasso_examples.zip
    226.6 KB · Views: 1,603
  • Picasso_all_library_files_v1_00.zip
    52.5 KB · Views: 1,879
Last edited:

Bel

Member
Licensed User
Hi
I cannot use this powerful libary in my project because it is not match with OKHttputill
Do you decide to update this library?
Right if you add add okHttp when run you have this error:
Yes,the other error is in loadurl method from picasso
what's solution?
 

MarcoRome

Expert
Licensed User
Longtime User
Hi
I cannot use this powerful libary in my project because it is not match with OKHttputill
Do you decide to update this library?

Yes,the other error is in loadurl method from picasso
what's solution?
For now you can use ImageDownloader or you see #AdditionalJar ( Look HERE you have big example by Erel about Picasso )
 
Top