Convert a Bitmap into a Byte Array

converting Java bitmap to byte array

Try something like this:

Bitmap bmp = intent.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
bmp.recycle();

Convert a bitmap into a byte array

There are a couple ways.

ImageConverter

public static byte[] ImageToByte(Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}

This one is convenient because it doesn't require a lot of code.

Memory Stream

public static byte[] ImageToByte2(Image img)
{
using (var stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
return stream.ToArray();
}
}

This one is equivalent to what you are doing, except the file is saved to memory instead of to disk. Although more code you have the option of ImageFormat and it can be easily modified between saving to memory or disk.

Source: http://www.vcskicks.com/image-to-byte.php

How to convert bitmap to byte array and byte array to bitmap in android?

Try this code

//For encoding toString
public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = android.util.Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
//For decoding
String str=encodedImage;
byte data[]= android.util.Base64.decode(str, android.util.Base64.DEFAULT);
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);

Converting bitmap to bytearray to string then convert back to bitmap is always null in Android

To properly convert a byte[] to a String, you should use Base64.encodeToString().

Documentation

How to convert Bitmap to Byte Array

Here is the link for more advanced image Loader library

the usage is as below:

first put this code in your main activity.

DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder().showStubImage(R.drawable.ic_stub).showImageOnFail(R.drawable.ic_error).showImageForEmptyUri(R.drawable.ic_empty_url).cacheInMemory().cacheOnDisc().build();
// Create global configuration and initialize ImageLoader with this
// configuration
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()).defaultDisplayImageOptions(defaultOptions).build();
ImageLoader.getInstance().init(config);

then use it in your class like this:

private ImageLoader imageLoader;

inside your onCreate() method

imageLoader = ImageLoader.getInstance();

then load image like this

imageLoader.displayImage(IMG_URL, imageView);

Android: Bitmap to Byte Array and back: SkImageDecoder::Factory returned null

You are passing Bitmap into Intent and get bitmap in next activity from bundle, but the problem is if your Bitmap/Image size is big at that time the image is not load in next activity.

Use below 2 Solutions to solve this issue.

1) First Convert Image into Byte Array and then pass into Intent and in next activity get byte array from Bundle and Convert into Image(Bitmap) and set into ImageView.

Convert Bitmap to Byte Array:-

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

Pass byte array into intent:-

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);

Get Byte Array from Bundle and Convert into Bitmap Image:-

Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);

2) First Save image into SDCard and in next activity set this image into ImageView.

convert to and from uncompressed bitmap to byte array

So the problem is with the bitmap in bytesToImage being incorrectly configured.

This requires the original bitmap to be passed in directly.

Here is the updated bytesToImage method which gives correct answer.

private static Bitmap bytesToImage(byte data[], Bitmap originalImage) {

Bitmap newBmp;
newBmp = Bitmap.createBitmap(originalImage.getWidth(), originalImage.getHeight(), originalImage.getConfig());


ByteBuffer buffer1 = ByteBuffer.wrap(data);

buffer1.rewind();
newBmp.copyPixelsFromBuffer(buffer1);

byte[] imageInByte = null;



ByteBuffer byte_buffer = ByteBuffer.wrap(data);



byte_buffer.rewind();



newBmp.copyPixelsFromBuffer(byte_buffer);


return newBmp;
}


Related Topics



Leave a reply



Submit