Skip to content

Instantly share code, notes, and snippets.

@The-LoneWolf
Created January 10, 2017 16:43
Show Gist options
  • Save The-LoneWolf/1afee9963369c9ca70b356fb7ee1ffa5 to your computer and use it in GitHub Desktop.
Save The-LoneWolf/1afee9963369c9ca70b356fb7ee1ffa5 to your computer and use it in GitHub Desktop.
Piccasso with Image Caching
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
Picasso.with(holder.img.getContext())
.load("file address on web")
.into(holder.img, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
BitmapDrawable btmpDr = (BitmapDrawable) holder.img.getDrawable();
Bitmap bmp = btmpDr.getBitmap();
ImageSaveTask task = new ImageSaveTask();
task.path = "file adress on memory";
task.execute(bmp);
}
@Override
public void onError() {
}
});
}
package ir.technopedia.tbzmed.helper;
import android.graphics.Bitmap;
import android.os.AsyncTask;
/**
* Created by user1 on 12/16/2016.
*/
public class ImageSaveTask extends AsyncTask<Bitmap, Void, Void> {
public String path;
@Override
protected Void doInBackground(Bitmap... voids) {
if (voids[0] != null) {
try {
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(path); //here is set your file path where you want to save or also here you can set file object directly
voids[0].compress(Bitmap.CompressFormat.PNG, 100, outputStream); // bitmap is your Bitmap instance, if you want to compress it you can compress reduce percentage
// PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment