Picasso default downloader - android

Just a simple question: what is the Picasso (2.5.*) default downloader?
I know I can use OkHttp
OkHttpClient okHttpClient = new OkHttpClient();
Picasso picasso = new Picasso.Builder(this)
.downloader(new OkHttpDownloader(okHttpClient))
.build();
but if I don't, whats the default Picasso downloader? And what's advantages to use OkHttp with Picasso?

The default class that implements Downloader interface is UrlConnectionDownloader if OkHttpClient class isn't in your class path.
The overview of OkHttp library should respond to your second question.

Related

Picasso doesn't cache image on disk

I have to use custom OkHttpClient so I can add headers to the image requests. The problem is Picasso won't cache any images on disk because of this. I've used setIndicatorsEnabled(true) to check caching and I see only red indicators. When I use default OkHttpDownloader all is ok. Below is my Picasso initialization code. So does anyone encounter the same problem?
public static void init(Context context) {
Picasso.Builder builder = new Picasso.Builder(context);
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new AuthInterceptor());
Downloader downloader = new OkHttpDownloader(client);
Picasso.setSingletonInstance(builder.downloader(downloader).build());
Picasso.with(context).setIndicatorsEnabled(true);
}
Also my image download code
public static void load(final ImageView imageView, final Image image) {
Picasso.with(imageView.getContext())
.load(image.getUrl())
.resize(400, 0)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.into(imageView);
}
Ah since this is happening when you change headers, you are most probably not setting the Cache-Control header
According to Jake wharton (One of the developer of Picasso)
Picasso doesn't have a disk cache. It delegates to whatever HTTP
client you are using for that functionality (relying on HTTP cache
semantics for cache control). Because of this, the behavior you seek
comes for free
Taken from Jake Wharton's answer here
Also,
If you never see a blue indicator, it's likely that your remote images
do not include proper cache headers to enable caching to disk

Picasso with OkHttp?

How to write picasso with Okhttp?
My code is given below but it is showing Error. By the way I am having confusion that picasso uses by default OkHttp or should I write it in code also?
OkHttpClient okHttpClient = new OkHttpClient();
RestAdapter restAdapter = new RestAdapter.Builder().setClient(new OkClient(okHttpClient)).build();
OkHttpDownloader downloader = new OkHttpDownloader(okHttpClient);
Picasso picasso = new Picasso.Builder(context).downloader(downloader).build();
Picasso.with(context).load("http://192.168.0.15:1337/offers/" + image_url.get(position)).resize(350, 100).centerCrop().into(holder.imageView);
No, you don't need to do things like that. OkHttp is a thing, which just make HTTP connection and loading files, for example, JSON easier. You should use it in separate thread, otherwise, you'll get an NetworkOnMainThreadException.
If you just need to download image, you won't need specific HTTP connection. Just use Picasso, for example, in onCreate() of the Activity, and enjoy the result.
All you need to do is:
Picasso.with(context)
.load("http://192.168.0.15:1337/offers/" + image_url.get(position))
.resize(350, 100)
.centerCrop()
.into(holder.imageView);

Where is OkHttpClient cache?

I am using OkHttpClient for the first time. It works very well, is very fast. So I understood that it uses an its own persistent cache, but when I request the folder where resides I receive null on getCache(). What is my misunderstanding?
OkHttpClient httpClient = new OkHttpClient();
Log.i("CACHE DIRECTORY", httpClient.getCache().getDirectory().toString());
Default impl of OkHttpClient has no cache. it provides a setCache method to customize one's own cache.

Loading images using picasso on a slow connection

I am using Picasso to load images for a listview.
The problem is internet connection is slow.
How can I change load timeout time in Picasso?
My code is :
Picasso.with(context)
.load(MainActivity.WEBSITE + book_item.Image)
.resize(final_thumb_width, final_thumb_height)
.into(new PicassoTarget(book_item,item.img, item.title));
You could possibly try something like this in your MainActivity's onCreate (or whereever you want to create the Picasso Builder
Picasso picasso;
OkHttpClient okHttpClient;
okHttpClient = new OkHttpClient();
okHttpClient.setConnectTimeout(10, TimeUnit.SECONDS);
picasso = new Picasso.Builder(this)
.downloader(new OkHttpDownloader(okHttpClient))
.build();
That should give Picasso a timeout of ten seconds. Configure it to your needs.
Full Disclosure: I don't use a timeout. I just noticed this in the API. This may be completely wrong lol.
You have two options:
Subclass a Downloader class. Check this for reference implementation
Preconfigure OkHttpClient with timeouts and pass it to Picasso

How to implement my own disk cache with picasso library - Android?

I'm using picasso library to load images for my app. But I don't how to implement my own disk (sdcard) caching with picasso library.
Picasso uses the HTTP client for disk caching and if one is already configured it will use that instead of installing its own.
For the built-in UrlConnection the docs for installing a cache are here: https://developer.android.com/reference/android/net/http/HttpResponseCache.html
If you are using OkHttp then you just call setCache:
http://square.github.io/okhttp/2.x/okhttp/com/squareup/okhttp/OkHttpClient.html#setCache-com.squareup.okhttp.Cache-
#Dax, to save files in custom cache directory using OkHttp, I would code something like this -
OkHttpClient okHttpClient = new OkHttpClient();
File customCacheDirectory = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/MyCache");
okHttpClient.setCache(new Cache(customCacheDirectory, Integer.MAX_VALUE));
OkHttpDownloader okHttpDownloader = new OkHttpDownloader(okHttpClient);
Picasso picasso = new Picasso.Builder(mainActivity).downloader(okHttpDownloader).build();
picasso.load(imageURL).into(viewHolder.image);
Hope this helps.

Categories

Resources