Reputation: 673
how can i get the byte[] of an image from android emulator SD card?
here's my current code:
File f = new File("/mnt/sdcard/test.jpg");
ImageView mImgView1 = (ImageView)findViewById(R.id.iv);
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
What i want is the byte format of the image how can i do this?
Upvotes: 1
Views: 1722
Reputation: 12809
Create a ByteArrayOutputStream
then call compress()
method of Bitmap
so the contents of bitmap will be passed in ByteArrayOutputStream
and converted to byte[]
public byte[] bitmapToByteArray(Bitmap b)
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
To apply,
Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath());
byte[] bitmapBytes = bitmapToByteArray(bmp);
Upvotes: 4
Reputation: 2805
You can convert Bitmap to byte[] like :
String imageInSD = Environment.getExternalStorageDirectory().getAbsolutePath() +"/Folder/" + filename + ".PNG";
Bitmap bmp = BitmapFactory.decodeFile(imageInSD);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Upvotes: 0