This is how you can convert a Bitmap
to a ByteArray
and a ByteArray
to a Bitmap
:
Convert Bitmap to ByteArray:
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Convert ByteArray to Bitmap:
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(Bitmap.createScaledBitmap(bmp, image.getWidth(), image.getHeight(), false));
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…