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,474
  • 13
  • 49
  • 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,206
  • 5
  • 27
  • 45
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
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
  • 13