Caching image for a week - android

I am developing an Android application which is displaying lots of images. I am using Picasso for loading the image and using its default caching strategy. Now what I want is to modify the caching and suppose if user has seen an image today it will be there in the cache for next seven days and each time user visit that particular page Picasso load the image from cache, after 7 days the image will be cleared from cache and do a fresh caching again. Someone, please help me I am lost.
private void PicassoConfig() {
Picasso.Builder builder = new Picasso.Builder(this);
builder.downloader(new OkHttp3Downloader (this, Constants.MAX_DISK_CACHE_SIZE));
Picasso built = builder.build();
Picasso.setSingletonInstance(built);
}
then
Picasso.get().load(cardList.get(i).
getImage()).noFade().priority(Picasso.Priority.HIGH).
placeholder(R.drawable.placehoder_image).
transform(new com.squareup.picasso.Transformation()
I want the image to be in the cache for a week.

Create a custom OkHttp interceptor and add to Picasso.
public Interceptor provideCacheInterceptor(final int maxDays) {
return new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Response response = chain.proceed(chain.request());
CacheControl cacheControl = new CacheControl.Builder()
.maxAge(maxDays, TimeUnit.DAYS)
.build();
return response.newBuilder()
.header(Constants.CACHE_CONTROL, cacheControl.toString())
.build();
}
};
}
Now, Add this to Gradle
compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.0.2'
Now attach Custom OkHttpClient to Picasso. More info here
okhttp3.OkHttpClient okHttp3Client = new okhttp3.OkHttpClient();
int MaxCacheDays = 7;
okHttp3Client.addNetworkInterceptor(provideCacheInterceptor(MaxCacheDays));
OkHttp3Downloader okHttp3Downloader = new OkHttp3Downloader(okHttp3Client);
Picasso picasso = new Picasso.Builder(context)
.downloader(new CustomOkHttp3Downloader(client))
.build();

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.

How to add Basic Authentication in Picasso 2.5.2 with OkHttp 3.2.0

I am using picasso 2.5.2 library to download bitmap from remote server, the image url requires basic authentication in header.
i have tried the following SO ansers but none of them work with the latest picasso and OkHttp libraries.
Answer - 1
Answer - 2
Answer - 3
Thanks in advance.
Try configuring an OkHttp3 client with authenticator, depending on your scheme and situation:
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.authenticator(new Authenticator()
{
#Override
public Request authenticate(Route route, Response response) throws IOException
{
String credential = Credentials.basic("user", "pass");
return response.request().newBuilder()
.header("Authorization", credential)
.build();
}
})
.build();
Then, use that client in forming your Picasso object, but with okhttp3 you will have to use a OkHttp3Downloader instead, like so:
Picasso picasso = new Picasso.Builder(context)
.downloader(new OkHttp3Downloader(okHttpClient))
.build();
You can get the OkHttp3Downloader from https://github.com/JakeWharton/picasso2-okhttp3-downloader

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

Http data caching in android application

I have searched for this but didn't got any appropriate ans. I am developing a social android app in which some feed data need to cache show that when user open this app and there is no network connection won't get blank screen. what is the best and fast access way to cache web data.
You will most likely parse the HTTP data into your own objects. So the question will rather be how to serialize these objects, and save them. These are the most common formats for object serialization:
a plain file, such as in XML or JSON format
a SQLite database
SharedPreferences (as a Set, so this will only work well if the order of the strings is able to be rebuilt later, such as them being in alphabetical order)
(These points look common, eh, #CommonsWare ;)
Facebook, for example, uses an SQLite database, at least for their iOS app.
If you are using retrofit then you can use cache interceptors.
Firstly declare cache memory size
int cacheSize = 10 * 1024 * 1024; // this is 10MB
Then create a cache object
Cache provideCache(MyApplication context) {
Cache cache = null;
try {
cache = new Cache(new File(context.getCacheDir(), "http-cache"), cacheSize);
} catch (Exception e) {
Log.e("Cache", "Error in creating Cache!");
}
return cache;
}
Then create 2 network interceptors for HTTP client ( One for Online, one for No network case)
Interceptor provideOnlineInterceptor(MyApplication context) {
return chain -> {
Response response = chain.proceed(chain.request());
CacheControl cacheControl;
if (Utilities.isNetworkConnected(context)) {
cacheControl = new CacheControl.Builder().maxAge(0, TimeUnit.SECONDS).build();
} else {
cacheControl = new CacheControl.Builder()
.maxStale(7, TimeUnit.DAYS)
.build();
}
return response.newBuilder()
.removeHeader(HEADER_PRAGMA)
.removeHeader(HEADER_CACHE_CONTROL)
.header(HEADER_CACHE_CONTROL, cacheControl.toString())
.build();
};
Interceptor provideOfflineInterceptor(MyApplication context) {
return chain -> {
Request request = chain.request();
if (!Utilities.isNetworkConnected(context)) {
CacheControl cacheControl = new CacheControl.Builder()
.maxStale(7, TimeUnit.DAYS)
.build();
request = request.newBuilder()
.removeHeader(HEADER_PRAGMA)
.removeHeader(HEADER_CACHE_CONTROL)
.cacheControl(cacheControl)
.build();
}
return chain.proceed(request);
};
Create Httpclient
OkHttpClient provideOkHttpClient(MyApplication context) {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
.addInterceptor(provideOfflineCacheInterceptor(context))
.addNetworkInterceptor(provideCacheInterceptor(context))
.cache(provideCache(context));
return httpClient.build();
}
Then finally add this httpclient to Retrofit instance
Retrofit provideRetrofit(OkHttpClient client,Gson gson) {
return new Retrofit.Builder().baseUrl(ApiConstants.BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build();
}
Hope this will work.
Please go through below link for more detail
Network caching using interceptors

Categories

Resources