Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawable to byte[]

I have an image from the web in an ImageView. It is very small (a favicon) and I'd like to store it in my SQLite database. I can get a Drawable from mImageView.getDrawable() but then I don't know what to do next. I don't fully understand the Drawable class in Android.

I know I can get a byte array from a Bitmap like:

Bitmap defaultIcon = BitmapFactory.decodeStream(in);  ByteArrayOutputStream stream = new ByteArrayOutputStream(); defaultIcon.compress(Bitmap.CompressFormat.JPEG, 100, stream);  byte[] bitmapdata = stream.toByteArray(); 

But how do I get a byte array from a Drawable?

like image 645
David Shellabarger Avatar asked Dec 14 '10 04:12

David Shellabarger


1 Answers

Drawable d; // the drawable (Captain Obvious, to the rescue!!!) Bitmap bitmap = ((BitmapDrawable)d).getBitmap(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); byte[] bitmapdata = stream.toByteArray(); 
like image 121
Cristian Avatar answered Sep 22 '22 06:09

Cristian