Created
September 20, 2016 01:57
-
-
Save agustinsivoplas/7261d7316e854a66a547e17da02476c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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