android: Stored Images in SqLite is taking 2x more space - android

I want to store jpg images in a SqLite database and I am using this code to convert bitmap to array of bytes.
public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, outputStream);
return outputStream.toByteArray();
}
the issue that image take more than 2x of it's size in database

Related

How to covert Image to byte[] or bitmap in Android Studio?

Can someone help me to solve a problem in Android Studio? I am creating an app in which I have a photo in JPEG format(Comming from the camera, not from file) and I want to convert it to bitmap.
You can use this method to convert image to byte[]. Like this:
public byte[] getBytesFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, stream);
return stream.toByteArray();
}
// Here image variable is a JPEG file/image.
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);

how to reduce size of image so that it can fit to blob type..?

I'm compressing an image by using this code
imageText=encodeToBase64(bitmap, Bitmap.CompressFormat.PNG,100);
but when I store this image into my database and attribute type blobmedium then it takes too much time for displaying (loading) the image.
Even if my original image size is 32 Kb, it stores as 259 Kb in the database.
When I use tinyblob or blob type for image attribute in database it stores the value but does not show any results in the UI.
How can I reduce the size of image so that it fit to tinyblobb or blob?
So that I can store and fetch image successfully.
use the below code to compress an image:
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
// call to base64 image conversion method:
String encodeToBase64 = bitmapToBase64(thumbnail);
and then encode it to the base64 format as:
private String bitmapToBase64(Bitmap bmp)
{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
Note that: A BLOB can be 65535 bytes maximum. If you need more consider using a MEDIUMBLOB for 16777215 bytes or a LONGBLOB for 4294967295 bytes

Bitmap compress doesn't changing bitmap byte size

I'm trying to reduce bitmap size by using compress method.
This is my code:
public Bitmap compressImage(Bitmap image) {
Bitmap immagex = image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Log.i("before compress", immagex.getByteCount()+"");
boolean compress = immagex.compress(Bitmap.CompressFormat.JPEG, 10, baos);
if(compress)
Log.i("after compress", immagex.getByteCount()+"");
else
Log.i("bad compress", "bad compress");
return immagex;
}
When i check my logs i get:
11-28 11:10:38.252: I/before compress(2429): 374544
11-28 11:10:38.262: I/after compress(2429): 374544
Why is the compress does not work?
UPDATE:
I tried this code:
public Bitmap compressImage(Bitmap image) {
Bitmap immagex = image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Log.i("before compress", immagex.getByteCount()+"");
boolean compress = immagex.compress(Bitmap.CompressFormat.JPEG, 10, baos);
Log.i("after compress 2", decodeSampledBitmapFromByte(image.getWidth(), image.getHeight(), baos.toByteArray()).getByteCount()+"");
return immagex;
}
Still the same byte count
11-28 11:33:04.335: I/before compress(3472): 374544
11-28 11:33:04.395: I/after compress 2(3472): 374544
Following is the code for reducing the bitmap size and convert it to base64,
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, 50, 50, true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
newBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] byteArray = baos.toByteArray();
String imageEncoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
Log.e("Base 64 String", imageEncoded);
Using Bitmap.compress() you just specify compression algorithm and by the way compression operation takes rather big amount of time. If you need to play with sizes for reducing memory allocation for your image, you exactly need to use scaling of your image using Bitmap.Options, computing bitmap bounds at first and then decoding it to your specified size.
The best sample that I found on StackOverflow is this one.
Strange out of memory issue while loading an image to a Bitmap object
Compression does not change the size of the bitmap. You should look at the size of the output byte stream which contains compressed data.

Image Size getting Smaller after Converting to Bytes

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.

Strange issue with Bitmap transparency while saving as BLOB

Trapped in a strange issue with Bitmap transparency, I have images with me and convert it in Blob and stored it in local Database.
I have this image
after storing it as Blob and fetching it, then image background gets filled
Any idea why this is happening, and to get through with it.
Thanks.
This is how i convert to Byte and stored to database
public byte[] BitmapToByte(Bitmap bitmap)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
return b;
}
This is how i m fetching Blob as Bitmap from database
mImageView.setImageBitmap(BitmapFactory.decodeByteArray(blob, 0, blob.length)); //blob by cursor
Got the solution, it is cause of using Android's Thumbnail folder(which creates additional preview and me passing thumbnail cursor to the image adapter). When i fetch image directly from the Database it is coming transparent.
My problem fixed just change the
Bitmap.CompressFormat.JPG to Bitmap.CompressFormat.PNG
public byte[] BitmapToByte(Bitmap bitmap)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//bitmap.compress(Bitmap.CompressFormat.JPG, 100, baos); //origin
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); //fixed
byte[] b = baos.toByteArray();
return b;
}

Categories

Resources