In my android application i want to convert image into byte array and encode into string so that i can save it on database. But after compressing image it's size becomes too small..i want to keep original size.. please help me..
final Bitmap image=(images.get(position));
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, bytes);
byte[] b = bytes.toByteArray();
encodedImageString = Base64.encodeToString(b, Base64.DEFAULT);
You can use the following to store it in the database without compression:
Bitmap bitmap; // obtain bitmap object
int size = bitmap.getRowBytes() * bitmap.getHeight();
ByteBuffer b = ByteBuffer.allocate(size);
bitmap.copyPixelsToBuffer(b);
byte[] bytes = new byte[size];
b.get(bytes, 0, bytes.length);
Then you can store bytes in the database.
Related
I've tried this code:
ByteArrayOutputStream bao = new ByteArrayOutputStream();
mylistImg.get(i).compress(Bitmap.CompressFormat.PNG, 100, bao);
byte[] ba = bao.toByteArray();
ba1 = Base64.encodeToString(ba, Base64.DEFAULT);
But it lowers the quality a lot. since I want to send a Base64 string to php, I must convert my Bitmap file to string without any quality reduction.
I've read these links but didn't get anything special!
How to convert Bitmap to string without losing quality
Edited
I'm sending ba1 variable to oracle database as a blob data
but when I see the result in database, the image quality is very low.
and I did not change anything inside my php code about image quality
Try this i just changed the way we get the byte array rather than using .compress method
//b is the Bitmap
Bitmap b=mylistImg.get(i);
//calculate how many bytes our image consists of.
int bytes = b.getByteCount();
//or we can calculate bytes this way. Use a different value than 4 if you don't use 32bit images.
//int bytes = b.getWidth()*b.getHeight()*4;
ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
b.copyPixelsToBuffer(buffer); //Move the byte data to the buffer
byte[] ba= buffer.array();
ba1 = Base64.encodeToString(ba, Base64.DEFAULT);
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().
In my android application i am taking image from camera , then i want to encode it without compression.Then after encoding i want to decode and show in image view
Following is the code, with compression and it is working perfectly(image is showing in image view)
Bitmap thumbnail;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.PNG, 1, bytes);
byte[] b = bytes.toByteArray();
String ImageString = Base64.encodeToString(b, Base64.DEFAULT);
byte[] bytarray = Base64.decode(ImageString, Base64.DEFAULT);
Bitmap bmimage = BitmapFactory.decodeByteArray(bytarray, 0,bytarray.length);
imageView11.setImageBitmap(bmimage);
But when i use following code image donot showup in imageView. please please please help
int bytes = b.getWidth()*b.getHeight()*4;
ByteBuffer buffer = ByteBuffer.allocate(bytes);
thumbnail.copyPixelsToBuffer(buffer);
byte[] b = buffer.array();
String ImageString = Base64.encodeToString(b, Base64.DEFAULT);
byte[] bytarray = Base64.decode(ImageString, Base64.DEFAULT);
Bitmap bmimage = BitmapFactory.decodeByteArray(bytarray, 0,bytarray.length);
imageView11.setImageBitmap(bmimage);
I am trying to convert byte array to bitmap to display an image in android app. But while converting it is returning the null value. I have used the following code:
operations = new DataBaseOperations();
byte image[] = operations.fetchimage(); // gets byte array from the database
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length, options);
Herebitmap is null, why?
Try this link. It will solve your problem
How to convert byte array to Bitmap
or just check this code
Bitmap bitmap = BitmapFactory.decodeFile("/path/images.jpg");
ByteArrayOutputStream blob = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, blob);
byte[] bitmapdata = blob.toByteArray();
//if bitmapdata is the byte array then getting bitmap goes like this
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length);
Returns The decoded bitmap, or null if the image could not be decode.
I've converted an bitmap image into string to save it:
............
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
Then I retrieve the bitmap from string to set an activity's background just like that:
byte[] temp = Base64.decode(encodedImage, Base64.DEFAULT);
Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0,
temp.length, options);
Drawable d = new BitmapDrawable(getResources(), bitmap);
getWindow().setBackgroundDrawable(d);
Everything works fine but the image quality reduces tremendously. I know it is because photo.compress(Bitmap.CompressFormat.JPEG, 100, baos); as I am compressing this image(though in best compressing resolution). So how can I do this without compressing the image? I have also tried the following code but it returns nothing
.......
Bitmap photo = extras.getParcelable("data");
int bytes = photo.getWidth() * photo.getHeight() * 4;
ByteBuffer buffer = ByteBuffer.allocate(bytes);
photo.copyPixelsToBuffer(buffer);
byte[] b = buffer.array();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
Also I am using sharedPreference to save the image. I thought of sqlite or internal storage also, but in both case compressing is needed as I found in internet