Skip to content

Instantly share code, notes, and snippets.

@ibnux
Created May 19, 2016 21:49
Show Gist options
  • Save ibnux/26db0c742e9d831ad36d34eb3c3ba2bf to your computer and use it in GitHub Desktop.
Save ibnux/26db0c742e9d831ad36d34eb3c3ba2bf to your computer and use it in GitHub Desktop.
Caching image with Picasso Library for Android
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;
import com.squareup.picasso.Transformation;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
/**
* Import Picasso to your project
* https://github.com/square/picasso
* same as using Picasso but different
* new Pikaso(this).load(URL).error(R.drawable.header).placeholder(R.drawable.header).into(ImageView);
*
* Not all function i handle, you can add new one
*/
public class Pikaso {
private ImageView view;
private Context c;
private String url, md5;
private Drawable error, placeholder;
private Transformation trasnform;
private int errorResID = R.drawable.ic_launcher,
placehoderResID = R.drawable.ic_launcher,
width=0,height=0;
private boolean cache = true;
private File file;
public Pikaso(){}
public Pikaso(Context c){
this.c = c;
}
public void mulaiAmbil(){
if(cache){
this.md5 = url;
if(trasnform!=null)
this.md5 += trasnform.key();
if(width>0 && height>0)
this.md5 += width+""+height;
this.md5 = Utils.md5(this.md5);
view.setTag(md5);
this.file = new File(c.getCacheDir(), md5);
if(file.exists()){
Utils.log("file exists "+md5+" "+url);
RequestCreator rc =Picasso.with(c)
.load(file);
if(error!=null)
rc.error(error);
else
rc.error(errorResID);
if(width>0 && height>0)
rc.resize(width,height);
if(placeholder!=null)
rc.placeholder(placeholder);
else
rc.placeholder(placehoderResID);
rc.into(view);
}else {
Utils.log("file not exists "+md5+" "+url);
kePicasso(null);
}
}else{
kePicasso(view);
}
}
private void kePicasso(ImageView v){
RequestCreator rc =Picasso.with(c)
.load(url);
if(error!=null)
rc.error(error);
else
rc.error(errorResID);
if(placeholder!=null)
rc.placeholder(placeholder);
else
rc.placeholder(placehoderResID);
if(trasnform!=null)
rc.transform(trasnform);
if(width>0 && height>0)
rc.resize(width,height);
rc.into(view, new Callback() {
@Override
public void onSuccess() {
if(cache){
try{
file.createNewFile();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
((BitmapDrawable)view.getDrawable()).getBitmap().compress(Bitmap.CompressFormat.PNG, 0, bos);
byte[] bitmapdata = bos.toByteArray();
//write the bytes in file
FileOutputStream fos = new FileOutputStream(file);
fos.write(bitmapdata);
fos.flush();
fos.close();
Utils.log("saved to file: "+ file.getAbsolutePath());
}catch (Exception e){
Utils.log("failed to save to cache: "+e.toString());
}
}
}
@Override
public void onError() {
}
});
}
public Pikaso resize(int width,int height){
this.width = width;
this.height = height;
return this;
}
public Pikaso with(Context c){
this.c = c;
return this;
}
public ImageView getView() {
return view;
}
public void into(ImageView view) {
this.view = view;
mulaiAmbil();
}
public Context getC() {
return c;
}
public Pikaso setC(Context c) {
this.c = c;
return this;
}
public String getUrl() {
return url;
}
public Pikaso load(String url) {
Utils.log("Pikaso load "+url);
this.url = url;
return this;
}
public Drawable getError() {
return error;
}
public Pikaso error(Drawable error) {
this.error = error;
return this;
}
public Drawable getPlaceholder() {
return placeholder;
}
public Pikaso placeholder(Drawable placeholder) {
this.placeholder = placeholder;
return this;
}
public Transformation getTrasnform() {
return trasnform;
}
public Pikaso transform(Transformation trasnform) {
this.trasnform = trasnform;
return this;
}
public int getErrorResID() {
return errorResID;
}
public Pikaso error(int errorResID) {
this.errorResID = errorResID;
return this;
}
public int getPlacehoderResID() {
return placehoderResID;
}
public Pikaso placeholder(int placehoderResID) {
this.placehoderResID = placehoderResID;
return this;
}
public Pikaso setCache(boolean cache) {
this.cache = cache;
return this;
}
public boolean isCache() {
return cache;
}
}
@rajesh2475
Copy link

Utils.md5(this.md5)??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment