Saving imageview's drawable to gallery after it was loaded using Glide

782 views
Skip to first unread message

japer...@gmail.com

unread,
Mar 28, 2015, 7:25:03 AM3/28/15
to glidel...@googlegroups.com
Hi,

I've got some trouble regarding the saving of an image to the user's gallery. Reason is that it says it can not cast GlideBitmapDrawable to BitmapDrawable. Code:

                Bitmap bmp = ((BitmapDrawable)imgDisplay.getDrawable()).getBitmap();
                MediaStore.Images.Media.insertImage(_activity.getContentResolver(),
                        bmp, "Titel" , "Beschrijving");


Thanks in advance,

Jasper

Róbert Papp

unread,
Mar 28, 2015, 7:49:18 AM3/28/15
to japer...@gmail.com, glidel...@googlegroups.com
It's simple, GlideBitmapDrawable is not a BitmapDrawable:

But it has a getBitmap() method, so if you really want to access the bitmap this way:
Bitmap bmp = ((GlideBitmapDrawable)imgDisplay.getDrawable()).getBitmap();

However I wouldn't recommend that, you can get the Bitmap in many waysin Glide, here's one (I let you extract classes/methods as you see fit):
//noinspection unchecked
new AsyncTask<FutureTarget<Bitmap>, Void, Bitmap>() {
@Override protected Bitmap doInBackground(FutureTarget<Bitmap>... params) {
try {
return params[0].get();
// get needs to be called on background thread
} catch (Exception ex) {
return null;
}
}
@Override protected void onPostExecute(Bitmap bitmap) {
if(bitmap == null) return;
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Titel", "Beschrijving");
}
}.execute(Glide.with(this).load(url).asBitmap().into(width, height));
// into needs to be called on main thread
The above compiles, but not tested, however based on my knowledge I don't see why it wouldn't work.
+Robi

--
You received this message because you are subscribed to the Google Groups "Glide" group.
To unsubscribe from this group and stop receiving emails from it, send an email to glidelibrary...@googlegroups.com.
To post to this group, send email to glidel...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/glidelibrary/f952704c-ba0b-4800-87bc-a36ae8df5546%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

japer...@gmail.com

unread,
Mar 28, 2015, 8:24:33 AM3/28/15
to glidel...@googlegroups.com, japer...@gmail.com
Awesome, thanks a lot Róbert!
Reply all
Reply to author
Forward
Message has been deleted
0 new messages