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;
}
Related
private String getBase64String() {
// give your image file url in mCurrentPhotoPath
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// In case you want to compress your image, here it's at 40%
// here i use JPEG image but now can anyone tell how can i convert any format image in String
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
You can use the Base64 Android class:
String ecodImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
You'll have to convert your image into a byte array though.Like:
Bitmap btm = BitmapFactory.decodeFile("/path/to/image.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
btm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //btm is the bitmap object
byte[] b = baos.toByteArray();
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);
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
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.
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);