Cached images in offline downloaded using custom downloader - android

In my application I'm using custom downloader to download images because server requires additional authorisation in request header. I'm trying to load downloaded images in offline mode, but when I'm using this custom downloader Picasso isn't loading images. Anyone could help?
OkHttpClient picassoClient = new OkHttpClient();
picassoClient.interceptors().add(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request().newBuilder()
.addHeader(RestUtils.HEADER, hash)
.build();
return chain.proceed(newRequest);
}
}
);
Picasso picasso = new Picasso.Builder(mContext)
.downloader(new OkHttpDownloader(picassoClient)).build();
picasso.setIndicatorsEnabled(true);
picasso.invalidate(RestUtils.getUrl(url));
if (DeviceUtility.isOnline(mContext)) {
picasso.load(RestUtils.getUrl(url))
.networkPolicy(
DeviceUtility.isOnline(mContext) ?
NetworkPolicy.NO_CACHE : NetworkPolicy.OFFLINE)
.resize(200, 200)
.centerCrop()
.into(viewHolder.mImgvPicture);
} else {
picasso.load(RestUtils.getUrl(url))
.networkPolicy(
DeviceUtility.isOnline(mContext) ?
NetworkPolicy.NO_CACHE : NetworkPolicy.OFFLINE)
.resize(200, 200)
.centerCrop()
.into(viewHolder.mImgvPicture);
}

You invalidate the URL right before getting it from cache:
picasso.invalidate(RestUtils.getUrl(url));
Try to invalidate only if the device is connected.

Related

Passing header to Picasso while viewing the image using url - Android

I need to pass a header when I try to show a image using Picasso.
Can any one suggest how to add header to picasso while viewing the image.
You can use downloader for that:
https://github.com/JakeWharton/picasso2-okhttp3-downloader
Example:
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request().newBuilder()
.addHeader("custom-header", "custom-header-value")
.build();
return chain.proceed(newRequest);
}
})
.build();
Picasso picasso = new Picasso.Builder(context)
.downloader(new OkHttp3Downloader(client))
.build();
Keep in mind that if you are using OkHttpClient already, you should use that instance or create new one using client.newBuilder(). This way, both instances will be using the same request queue.

How to get errors from picasso2-okhttp3-downloader

I am using Picasso to download images and show them from the server. I need to set basic Aucathention to my requests. And because of that i used this:
'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
and my code is:
String url = "images/download/" + images.getiId();
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request().newBuilder()
.addHeader("user_auth", user_auth)
.addHeader("user_password", user_pass)
.build();
return chain.proceed(newRequest);
}
})
.build();
Picasso picasso = new Picasso.Builder(context)
.downloader(new OkHttp3Downloader(client))
.build();
picasso.load(url)
.error(R.drawable.error_icon_128)
.into(view);
}
but every time it can't download images And I can't see response code from the server. How can I see response code?
Thank you.

Make picasso to play well with headers like "if-modified-since" and "ETag"

In my app image contents are changing frequently (like once in a week) and imageurl remains the same. I am using picasso for image loading. My query is to can I set age to cache-entry and use the headers like 'if-modified-since' and 'ETag'?
This is how I resolved it.
Interceptor interceptor = new Interceptor() {
public static final String TAG = "RequestInterceptor";
#Override
public Response intercept(Chain chain) throws IOException {
final Request original = chain.request(); //Original request initiated
final Response response; //Response for given url
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder().header("if-modified-since", last-modified-date-for-image).method(original.method(), original.body());
Request request = requestBuilder.build();
response = chain.proceed(request);
Log.d(TAG, "Intercepting status code : " + response.code());
Log.d(TAG, "Intercepting requests : Url :" + request.url().toString());
Log.d(TAG, "Intercepting requests : if-modified-since : " + response.header("if-modified-since".toLowerCase()));
/* if response code is 200 means we have updated image otherwise return older one.*/
if (response.code() == 200) {
return response;
} else {
return chain.proceed(original);
}
}
};
//Create OkHttpClient for picasso downloader and add request interceptor.
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(interceptor).build();
//Instantiate okHttp3Downloader with okHttpClient
OkHttp3Downloader okHttpDownloader = new OkHttp3Downloader(okHttpClient);
//Create picasso instance
picasso = new Picasso.Builder(this).downloader(okHttpDownloader).build();
//load image in imageview 'img'
picasso.load("image url").into(img);
You can use 'ETag' instead of 'if-modified-since'.
Known issue : images are not getting loaded when device is offline. Please load it from cache forcefully.

Picasso 2.5.2 image not loading with network policy

I am trying to load an image from url, but adding authorization token. Picasso does not want at all to load it if i add networkPolicy(NetworkPolicy.OFFLINE) is there any problem with it ?
my code :
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
#Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request().newBuilder()
.addHeader("Authorization", "Bearer " + userPrefs.accessToken().get())
.build();
return chain.proceed(newRequest);
}
})
.build();
Picasso picasso = new Picasso.Builder(getActivity())
.downloader(new OkHttp3Downloader(client))
.build();
picasso
.load(URL)
.networkPolicy(NetworkPolicy.OFFLINE)
.into(ivUserImage, new Callback() {
#Override
public void onSuccess() {
Toast.makeText(getActivity(), "+", Toast.LENGTH_SHORT).show();
}
#Override
public void onError() {
Toast.makeText(getActivity(), "-", Toast.LENGTH_SHORT).show();
}
});
Yes, it is
We have three enums to pass to networkPolicy
NO_CACHE
Skips checking the disk cache and forces loading through the network.
NO_STORE
Skips storing the result into the disk cache.
OFFLINE
Forces the request through the disk cache only, skipping network.
So if we use OFFLINE, It will skip the Network.
Also please read this API for Picasso
The easy way to use the offline policy
Picasso.with( getApplicationContext() ).load( link ).networkPolicy( NetworkPolicy.OFFLINE ).placeholder( R.drawable.notification ).into( imageView, new Callback() {
#Override
public void onSuccess()
{
}
#Override
public void onError() {
Picasso.with( getApplicationContext() ).load( link ).placeholder( R.drawable.notification ).into( imageView );
}
} );

Picasso memory/disk cache

I use Picasso to download images from the web, and display them in a RecyclerView.
private Picasso createPicasso(Context context){
OkHttpClient picassoClient = new OkHttpClient();
picassoClient.interceptors().add(new Interceptor() {
#Override
public Response intercept(Interceptor.Chain chain) throws IOException {
try {
Map authHeaders = BackendServiceClient.buildAuthHeaders();
Request newRequest = chain.request().newBuilder()
.addHeader("Authorization", (String) authHeaders.get("Authorization"))
.build();
return chain.proceed(newRequest);
} catch (CredentialNotStoredException e) {
e.printStackTrace();
}
return chain.proceed(chain.request().newBuilder().build());
}
});
return new Picasso.Builder(context)
.downloader(new OkHttpDownloader(picassoClient))
.build();
}
Usage:
ImageDownloader.getSharedInstance().getPicasso(context)
.load(url)
.placeholder(R.drawable.head_big) //
.error(R.drawable.head_big) //
.tag(context)
.into(holder.personPhoto);
Downloading and displaying the images works as expected, but if I scroll through the list, the images gets fetched from the web again and were not cached.
How is it possible to always cache them on the disk and memory.
Switched to Glide which improves memory footprint and caching works out of the box.
https://github.com/bumptech/glide
http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en

Categories

Resources