load bmp image from byte array in android? - android

I am new to android,I developed one sample application which load the image from sd card and display as a bitmap image using image view controll.
Now i want to modify my application like load the bmp from byte array i have the raw image ,width and height ,do any one have sample for this?

Use below Code for Convert Byte Array to Bitmap and display this bitmap into ImageView.
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
and see below SO link for more information.
Convert ByteArray to Bitmap

I think you can use BitmapFactory.
public static Bitmap decodeByteArray (byte[] data, int offset, int length)
For more info, look here

if your image is in Drawable folder try this code
Drawable drawable= getResources().getDrawable(R.drawable.yourimage);
//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 = stream.toByteArray();

Related

Dart/Flutter Image library decode Bitmap fails

I am trying to read a bitmap generated on the platform site
here is the code I use to generate a byte array in android:
ByteBuffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
bitmap.copyPixelsToBuffer(byteBuffer);
return byteBuffer.array();
When I try to decode it with the image library it fails (dart code):
Image img = decodeBmp(bitmapData);
The method 'getBytes' was called on null.
bitmap data is not empty and looks proper for me
If I instead compress image on the android side to png or jpg than decodeImage (or -Png or -Jpg) works
but I dont want to use bitmap.compress
this code would work with compress
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bufferedBmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imageData = baos.toByteArray();
return imageData;

Byte array to bitmap converting issue

I am trying to implement Converting bitmap to byteArray android. I need to convert my byte[] to bitmap without compression. But everytime im getting whole black image. How to do it?
What I am doing:
int bytes = bmp.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
bmp.copyPixelsToBuffer(buffer);
byte[] resarray = buffer.array();
And here how I get it to bitmap:
BitmapFactory.Options options = new
BitmapFactory.Options();
options.inScaled = false;
Bitmap bmp = BitmapFactory.decodeByteArray(barray,0, barray.length,options);
ImageView imageView = (ImageView) findViewById(R.id.resdetayimage);
imageView.setImageBitmap(bmp);`
EDIT
Bitmap factory decode only compressed things. Thats why my code not work. So I need something like:
Bitmap.Config configBmp = Bitmap.Config.valueOf(bitmap.getConfig().name());
Bitmap bitmap_tmp = Bitmap.createBitmap(width, height, configBmp);
ByteBuffer buffer = ByteBuffer.wrap(byteArray);
bitmap_tmp.copyPixelsFromBuffer(buffer);
But the code still not working. How can I implement this ? I got byte[] from intent.
Convert bitmap to byteArray
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, bStream);
byte[] mByteArray = bStream.toByteArray();
Convert byteArray to Bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(mByteArray , 0, mByteArray.length);
Use copyPixelsFromBuffer() as it is the reverse of copyPixelsToBuffer().

Convert PNG image to Base64

I am converting drawable resource png image to bitmap and then converting that bitmap to base64 and sending it to server by web service.
The image is stored on server at some address and in response i am getting URL where the image is stored.
The problem is after sending image to server the url which i am getting with that url i am setting image to other imageview but the transperent part of image is becoming black colored,
i think the problem is with converting png image to base64,
my code:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
bitmap = Bitmap.createScaledBitmap(bitmap, 300, 300, false);
mImageView.setImageBitmap(bitmap);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, bao);
byte[] ba = bao.toByteArray();
base64Image = Base64.encodeToString(ba, Base64.DEFAULT);
please give me solution ASAP
Everything you are doing seems right. Depending on the version of Android you are compiling with, decodeResource can have different default behavior. You can force the BitmapFactory to use an alpha channel when decoding with the following code.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inDither = false;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher, options);

ParseFile on Android

I'm using the Parse SDK to build my app.
I can easily get a Bitmap out of the gallery by using Intents. I want to use them as a profile picture for my users.
However, in order to upload it I must convert it into a byte array. Also, when I download it it comes in the form of a byte array, and I must convert it back to a Drawable.
In order to convert it into a byte array, I'm doing this:
public static byte[] bitmapToByteArray(Bitmap bmp)
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
I cannot find how to convert this byte[] back into a Bitmap without having to save it first. How can this process be achieved?
Just do the following:
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);

How to convert image file data in a byte array to a Bitmap?

I want to store image in SQLite DataBase.
I tried to store it using BLOB and String, in both cases it store the
image and can retrieve it but when i convert it to Bitmap using
BitmapFactory.decodeByteArray(...) it return null.
I have used this code, but it returns null
Bitmap bitmap = BitmapFactory.decodeByteArray(blob, 0, blob.length);
Just try this:
Bitmap bitmap = BitmapFactory.decodeFile("/path/images/image.jpg");
ByteArrayOutputStream blob = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /* Ignored for PNGs */, blob);
byte[] bitmapdata = blob.toByteArray();
If bitmapdata is the byte array then getting Bitmap is done like this:
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);
Returns the decoded Bitmap, or null if the image could not be decoded.
The answer of Uttam didnt work for me. I just got null when I do:
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);
In my case, bitmapdata only has the buffer of the pixels, so it is imposible for the function decodeByteArray to guess which the width, the height and the color bits use. So I tried this and it worked:
//Create bitmap with width, height, and 4 bytes color (RGBA)
Bitmap bmp = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888);
ByteBuffer buffer = ByteBuffer.wrap(bitmapdata);
bmp.copyPixelsFromBuffer(buffer);
Check https://developer.android.com/reference/android/graphics/Bitmap.Config.html for different color options

Categories

Resources