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,702
  • Picasso_all_library_files_v1_00.zip
    52.5 KB · Views: 2,000
Last edited:

a2stepper

Member
Licensed User
Longtime User
thanks for the info for the tutorial for the new .jar. i did download the version 2.3.3, renamed it and moved to the library folder.
not nothing keep getting picasso has stopped working. i have tried in the two sample files you had and also the file
i've been working on with this error on all
is there something else i need to do??
thanks again
paul
 

Shahid Saeed

Active Member
Licensed User
Longtime User
thanks for the info for the tutorial for the new .jar. i did download the version 2.3.3, renamed it and moved to the library folder.
not nothing keep getting picasso has stopped working. i have tried in the two sample files you had and also the file
i've been working on with this error on all
is there something else i need to do??
thanks again
paul
If you want to use the latest jar file you cannot use it as Library Wrapper you will have to include JAR file directly into main activity. Just go through this tutorial and it will work: http://www.b4x.com/android/forum/th...onaljar-and-javaobject-picasso.40904/#content
 

migrec

Member
Licensed User
Longtime User
Very good lib, extremely time-saving! :D
sometimes Picasso1.LoadUrl("").Fit.IntoImageView(ImageView1) won't resize the image though, any reason why? maybe I'm using it wrong..
 

Shahid Saeed

Active Member
Licensed User
Longtime User
Very good lib, extremely time-saving! :D
sometimes Picasso1.LoadUrl("").Fit.IntoImageView(ImageView1) won't resize the image though, any reason why? maybe I'm using it wrong..
Fit.IntoImageView will fit the picture as per the dimensions of the imageView; therefore the picture may look stretched. If you want to load picture proportionally try the following re-size method:-

B4X:
Picasso1.LoadUrl("").Resize(ImageWidth,ImageHeight).CenterCrop.IntoImageView(ImageView1)
 

Vincenzo Fabiano

Member
Licensed User
Longtime User
No update of the wrapper for this magnificent library ?? So the method with the external jar .. but with the wrapper would be so much easier and complete! This library I consider it one of those basics! Please @warwound update it! If necessary, enter it below donation, I will be happy to repay your work!
 

scsjc

Well-Known Member
Licensed User
Longtime User
hello,
can force clear cache, to reload a new image ????
i test SKIPCACHE... and some times is clear and some times not ????
 
Last edited:

a2stepper

Member
Licensed User
Longtime User
do you have the latest working version for basic 4 android. i have parts but none are working very good.
thanks.
paul
 

Douglas Farias

Expert
Licensed User
Longtime User
i have for with picasse to load the images at my imageview ok this works 2x times
my for have 100 images i load 100 images one time, but later a time waiting 15 seconds on app when i try load my for again dont load the images
the picasso dont work more i dont see any images why? i have try use skipcache and i try to load a color on my imageview before load the new images but dont load =( picass works only 1 time here
 

Shahid Saeed

Active Member
Licensed User
Longtime User
i have for with picasse to load the images at my imageview ok this works 2x times
my for have 100 images i load 100 images one time, but later a time waiting 15 seconds on app when i try load my for again dont load the images
the picasso dont work more i dont see any images why? i have try use skipcache and i try to load a color on my imageview before load the new images but dont load =( picass works only 1 time here

Try using the latest library of picasso; If you want to use the latest jar file you cannot use it as Library Wrapper you will have to include JAR file directly into main activity. Just go through this tutorial and it will work: http://www.b4x.com/android/fo...onaljar-and-javaobject-picasso.40904/#content
 

Jerez

Active Member
Licensed User
Longtime User
Hi,

I can't stream this image URL with Picasso... why? is too big?

I can stream other pics but not this url... and this url return a Jpeg image data.

http://50.197.211.181:9831/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=user&pwd=foscam


B4X:
DownloadImage(Url, imgCamera)

Sub DownloadImage(url AsString, imgView AsImageView)

Dim imgDownload As Picasso
imgDownload.Initialize
imgDownload.LoadUrl(url).Resize(imgView.Width, imgView.Height).CenterCrop.IntoImageView(imgView)

End Sub

With ImageDownloader Module i haven't problem.

please help
 

Douglas Farias

Expert
Licensed User
Longtime User
Hi,

I can't stream this image URL with Picasso... why? is too big?

I can stream other pics but not this url... and this url return a Jpeg image data.

http://50.197.211.181:9831/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=user&pwd=foscam


B4X:
DownloadImage(Url, imgCamera)

Sub DownloadImage(url AsString, imgView AsImageView)

Dim imgDownload As Picasso
imgDownload.Initialize
imgDownload.LoadUrl(url).Resize(imgView.Width, imgView.Height).CenterCrop.IntoImageView(imgView)

End Sub

With ImageDownloader Module i haven't problem.

please help

what happens if u add .jpg on the end of url?
 

Jerez

Active Member
Licensed User
Longtime User
what happens if u add .jpg on the end of url?

No man... does not work.
Also... in another project i'm loading about 5 jpegs 640x480 @ 200~300 bk each... and 4 of 5 images are loaded (random). always one fail.
Any other library that can download images, resize and crop?

** This library is an abandonware **
 

Jerez

Active Member
Licensed User
Longtime User
Erel, any chance you can implement a "dislike" button?

@moster67: You can say with your own words "dislike"... no need a button for it.

It's a great library! but i would like an answer from the developer. An "I can't" or "I haven't time" is enough for me.
 

Blue.Sky

Active Member
Licensed User
Longtime User
Hi
I have a many image link that should be download
I used imageDownloader but now i want to use Picasso
Example :
For i = 0 to ls.size - 1
dim p as picasso
p.initilize
p.loadurl(ls.get(i))
next

but not work
i use defaultTarget but it get error
Do you have a solution?
 

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?
 

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?
Right if you add add okHttp when run you have this error:


** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
main_activity_create (B4A line: 31)
Picasso1.Initialize
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/squareup/okhttp/HttpResponseCache;
at com.squareup.picasso_OkHttpLoader.<init>(OkHttpLoader.java:58)
at com.squareup.picasso_OkHttpLoader.<init>(OkHttpLoader.java:35)
at com.squareup.picasso_OkHttpLoader.<init>(OkHttpLoader.java:25)
at com.squareup.picasso.Utils$OkHttpLoaderCreator.create(Utils.java:287)
at com.squareup.picasso.Utils.createDefaultLoader(Utils.java:199)
at com.squareup.picasso.Picasso$Builder.build(Picasso.java:617)
at com.squareup.picasso.Picasso.with(Picasso.java:542)
at uk.co.martinpearman.b4a.squareup.picasso.Picasso.Initialize(Picasso.java:51)
at b4a.example.pic.main._activity_create(main.java:351)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at b4a.example.pic.main.afterFirstLayout(main.java:102)
at b4a.example.pic.main.access$000(main.java:17)
at b4a.example.pic.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6912)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.squareup.okhttp.HttpResponseCache" on path: DexPathList[[zip file "/data/app/b4a.example.pic-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
... 23 more
Suppressed: java.lang.ClassNotFoundException: com.squareup.okhttp.HttpResponseCache
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 24 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
 
Top