1

I want to take the image from drawable folder but it should be in the byte[] format.So that i can save that byte[] into the database.I have gone through links but that is taking image from drawable folder in String or Drawable format. Any suggestion for me plz..

Narendra Pal
  • 6,264
  • 11
  • 47
  • 85

2 Answers2

4

Obtain the drawable using getResources() method.

Drawable drawable= getResources().getDrawable(R.drawable.image1);

Type cast to BitmapDrawable,

Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();

Write a compressed version of the bitmap to the specified outputstream via compress method.

ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
byte[] buffer= out.toByteArray();
Nilabja
  • 4,037
  • 5
  • 25
  • 40
KV Prajapati
  • 92,042
  • 19
  • 143
  • 183
0

You probably want to use the openRawResource function from the Resources class. It will give you an InputStream. You can then use the stream to get a raw byte array (using the read function).

MJD
  • 1,183
  • 7
  • 12