64

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?

manfcas
  • 1,933
  • 7
  • 28
  • 47
David Shellabarger
  • 1,523
  • 2
  • 15
  • 23

6 Answers6

147
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();
Ilya Saunkin
  • 18,934
  • 9
  • 36
  • 50
Cristian
  • 198,401
  • 62
  • 356
  • 264
22

Thanks all and this solved my problem.

Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.my_pic);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitMapData = stream.toByteArray();
Randula
  • 1,537
  • 15
  • 11
  • i saved bitMapData in database as BLOB, again when i regain my drawable from BLOB, background becomes black. Can u help me? i want background transparent as it was before saving to database. – Sazzad Hissain Khan Sep 12 '14 at 10:30
  • I think its a different issue that occur parallel to that process. Its great if you can provide more details to reproduce and check. – Randula Sep 20 '14 at 18:42
  • 1
    it was happening when saving with JPEG format, but i changed to PNG its working fine... – Sazzad Hissain Khan Sep 21 '14 at 04:47
10
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tester);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitMapData = stream.toByteArray();
Kalpesh
  • 1,767
  • 19
  • 29
2

Here is my Utility function which can be copy-pasted

/**
 * @param ctx the context
 * @param res the resource id
 * @return the byte[] data of the fiven drawable identified with the resId
 */
public static byte[] getDrawableFromRes(Context ctx, @DrawableRes int res) {
    Bitmap bitmap = BitmapFactory.decodeResource(ctx.getResources(), res);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    return stream.toByteArray();
}
Szabolcs Becze
  • 507
  • 1
  • 5
  • 10
0

If Drawable is an BitmapDrawable you can try this one.

long getSizeInBytes(Drawable drawable) {
    if (drawable == null)
        return 0;

    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
    return bitmap.getRowBytes() * bitmap.getHeight();
}

Bitmap.getRowBytes() returns the number of bytes between rows in the bitmap's pixels.

For more refer this project: LazyList

Favas Kv
  • 2,961
  • 2
  • 28
  • 38
  • 1
    This post is being automatically flagged as low quality because it is only code. Would you mind expanding it by adding some text to explain how it solves the problem? – gung - Reinstate Monica Jun 13 '14 at 15:04
-2
File myFile = new File(selectedImagePath);

byte [] mybytearray  = new byte [filelenghth];

BufferedInputStream bis1 = new BufferedInputStream(new FileInputStream(myFile));

bis1.read(mybytearray,0,mybytearray.length);

now the image is stored in the bytearray..

Samet Atdag
  • 982
  • 6
  • 21
Abhishek Susarla
  • 578
  • 1
  • 6
  • 12