Skip to content

Instantly share code, notes, and snippets.

@agustinsivoplas
Created September 20, 2016 01:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agustinsivoplas/7261d7316e854a66a547e17da02476c3 to your computer and use it in GitHub Desktop.
Save agustinsivoplas/7261d7316e854a66a547e17da02476c3 to your computer and use it in GitHub Desktop.
import android.graphics.Bitmap;
import android.widget.ImageView;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import java.io.FileOutputStream;
import java.io.IOException;
public class GlideFileTarget extends SimpleTarget<Bitmap> {
String fileName;
Bitmap.CompressFormat format;
int quality;
ImageView imageViewTarget;
public GlideFileTarget(String fileName, ImageView imageViewTarget) {
this(fileName, imageViewTarget, Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL, Bitmap.CompressFormat.JPEG, 70);
}
public GlideFileTarget(String fileName, ImageView imageViewTarget, int width, int height) {
this(fileName, imageViewTarget, width, height, Bitmap.CompressFormat.JPEG, 70);
}
public GlideFileTarget(String fileName, ImageView imageViewTarget, int width, int height, Bitmap.CompressFormat format, int quality) {
super(width, height);
this.fileName = fileName;
this.format = format;
this.quality = quality;
this.imageViewTarget = imageViewTarget;
}
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
try {
imageViewTarget.setImageBitmap(bitmap);
if (fileName != null) {
FileOutputStream out = new FileOutputStream(fileName);
bitmap.compress(format, quality, out);
out.flush();
out.close();
onFileSaved();
}
} catch (IOException e) {
e.printStackTrace();
onSaveException(e);
}
}
public void onFileSaved() {
// do nothing, should be overriden (optional)
}
public void onSaveException(Exception e) {
// do nothing, should be overriden (optional)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment