how to convert base64 string image to blob in android? - android

I have a Base64 string image .I want to convert it into blob type and save it in database..
I have
String base64Image="iVBORw0KGgoAAAANSUhEUgAAAGAAAACgCAYAAADzcGmMAAAACSV...";
This is what I tried..
byte[] byteImage=org.apache.commons.codec.binary.Base64.decodeBase64(befImage.getBytes());
json.put("during_unloading_photo", byteImage);
where json is my JSONObject and
during_unloading_photo is the column name which is blob type.

for saving blob into DataBase you have to convert your base64 satring image to Byte[] and then you can save byte[] into db.
for converting base64 stringto bitmap
public static Bitmap decodeBase64Profile(String input) {
Bitmap bitmap = null;
if (input != null) {
byte[] decodedByte = Base64.decode(input, 0);
bitmap = BitmapFactory
.decodeByteArray(decodedByte, 0, decodedByte.length);
}
return bitmap;
}
and now you can convert this Bitmap to Byte[] Like:
public static byte[] getBytesFromBitmap(Bitmap bitmap) {
if (bitmap!=null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
return stream.toByteArray();
}
return null;
}
then finally you can save this Byte[] to Db.make sure your column name have type with blob
enjoy your code:)-

getBytes gives you the bytes of the string, it does not do base64 decoding. You will need to use a Base64 decoder to do the conversion. A little search will show up several options you can use.

Related

How to convert Bitmap into Base64 String without compressing? [duplicate]

This question already has an answer here:
How to convert a image into Base64 string without compressing the image?
(1 answer)
Closed 3 years ago.
I am trying to convert a bitmap image into base64 String using this code. I and getting a very low-quality image. How can I get a good quality image after convert bitmap into Base64 Sring
public String BitMapToString(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
base64Image = Base64.encodeToString(b, Base64.DEFAULT);
return base64Image;
}
If I have a 5MB image. After convert, I am getting the only 160KB image. But in my case, I don't want to compress my image too much I just want to get Base64 String based on Bitmap image only JPEG format, not any other. Please help me with this.
Try this:
public String getBase64Image(Bitmap bitmap) {
try {
ByteBuffer buffer =
ByteBuffer.allocate(bitmap.getRowBytes() *
bitmap.getHeight());
bitmap.copyPixelsToBuffer(buffer);
byte[] data = buffer.array();
return Base64.encodeToString(bytes, Base64.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

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

I am developing an Android app. In my app, I am working with bitmaps. What I am doing is I am converting bitmap to byte array. Then I convert byte array to string. I need to do it some reasons. Converting bitmap to byte array is working. Byte array to string is also converted. Then problem start when I work with that converted string. I am converting that string back to byte array. Then I convert that byte array back to bitmap. But bitmap is always null.
This is my function that convert bitmap to byte array
public static byte[] ConvertBitmapToByteArray(Bitmap bitmap)
{
if(bitmap==null)
{
return null;
}
else{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
}
This is function that converts byte array to bitmap
public static Bitmap ConvertByteArarysToBitmap(byte[] byteArray)
{
if(byteArray!=null)
{
return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
}
else{
return null;
}
}
These are the steps I am converting
byte[] byteArray = Helper.ConvertBitmapToByteArray(Bitmap bitmap);
//convert byte array to string
String imageString = new String(byteArray,"UTF-8");
//I convert that string back to byte array
byte[] reconvertedByteArray = imageString.getBytes("UTF-8");
Bitmap reconvertedBitmap = Helper.ConvertByteArarysToBitmap(reconvertedByteArray);
In my code, the last reconvertedBitmap is always null. What is wrong with my code? What is the correct way to convert byte array to string and then convert back that string to byte array. What is missing in my code?
To properly convert a byte[] to a String, you should use Base64.encodeToString().
Documentation

Android: Recover image bitmap from representation as decoded string [duplicate]

In Java server I fetch image from external service URL like:
InputStream in = new java.net.URL(imageWebServiceURL).openStream();
String resultToCleint = org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(IOUtils.toByteArray(in));
Then on Android I parse it like:
byte[] imageAsBytes = Base64.decode(resultToCleint.getBytes(), Base64.DEFAULT);
imageView.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
Result: Image not displayed, ain't errors/exceptions neither on server nor on client.
What is the problem here?
EDIT: On android I use class android.util.Base64
Thanks,
Use Picasso library to load image:
You just need to add 1 line of code to show the image on ImageView
//Loading image from below url into imageView
Picasso.with(this)
.load("YOUR IMAGE URL HERE")
.into(imageView);
You can learn more from here
use this to convert to base 64
public static String uploadPic(Bitmap bm) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String encoded = ""+ Base64.encodeToString(byteArray, Base64.DEFAULT);
return encoded;
}
check if image is uploaded then using volley String request object download the string response using this code convert it back.
public Bitmap StringToBitMap(String encodedString){
try {
byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
} catch(Exception e) {
e.getMessage();
return null;
}
}
As commented, let's assume base64Content is the base64 string responsed from your web service/server-side app, you can refer to the following sample code:
String base64Content = jsonObject.getString("Base64Content");
byte[] bytes = Base64.decode(base64Content, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Moreover, if your server compressed reponse data either by gzip or deflate, your client app must decompress the data first.
Hope this helps!

android bitmap isn't created from base64

I have an Android application which sends an image to a web service. I want to send the same photo back from the web service to Android.
I made a test program to compare the base64 data that's sent from Android to the server and the base64 that's sent back from server to Android -- they are exactly equal.
I want to use the base 64 string to create a bitmap, so I tried this:
String image = client1.getBaseURI("restaurantFoods/OneFood/"
+ this.getID() + "/getImage");
byte[] decodedString = Base64.decode(image, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,
decodedString.length);
if(decodedByte == null){
Log.d(this.getFoodItem().getName(), image);
Log.d("isNull", "Yes");
}
else{
Log.d("isNull", "No");}
I keep getting null because the log just prints "YES".
Can anyone please help?
If you want to know how I encode the image it is as follows:
private String getBase64(Bitmap bitmap) {
String imgString = Base64.encodeToString(getBytesFromBitmap(bitmap),
Base64.NO_WRAP);
return imgString;
}
private byte[] getBytesFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 70, stream);
return stream.toByteArray();
}
Bitmap icon = BitmapFactory.decodeResource(this.getResources(),
R.drawable.pizza);
String iconBase64 = this.getBase64(icon);
Try this to bitmap;
public Bitmap convert(String img){
byte[] b = Base64.decode(img, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
And this to String
public String convert(Bitmap bm, int quality){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, quality, baos);
byte[] byt = baos.toByteArray();
bm.recycle();
return Base64.encodeToString(byt, Base64.DEFAULT);
}
Really I don't see any real problems with your code, but these have worked for me so I suggest that you try them and see if that is actually your problem.

Decoded String Image Can't Be Display As BitMap in Android

Below is the portion of code I used to encode the image file from bitmap to base64 string by import android.util.Base64 in my android project:
`
Bitmap img_bmp=BitmapFactory.decodeStream(getContentResolver().
openInputStream(this.browseImageURI));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
img_bmp.compress(Bitmap.CompressFormat.JPEG, 30, baos);
byte[] image = baos.toByteArray();
String profile_img = Base64.encodeToString(image, Base64.DEFAULT);
`
The string profile_img will be saved in mysql database as a string.
When I retrieve the string value from database, I will decode image string from String to Bitmap by using the below code:
`
Intent i = getIntent(); //pass the value from previous activity
str_img= i.getStringExtra("img");
img_bm = StringToBitMap(str_img);
imgview = (ImageView)findViewById(R.id.imageView1);
imgview.setImageBitmap(img_bm); // display the image
//Function to convert string to bitmap
public Bitmap StringToBitMap(String image){
try{
byte [] encodeByte=Base64.decode(image,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
}catch(Exception e){
e.getMessage();
return null;
}
}
`
I expected it will display the image, however the image is not displayed and I get a log message from the Base64 decoder "--- decoder->decode returned false".
Can someone help me to figure out what is wrong in my code?
And do anyone know how to convert the base64 string image (pass from JSON to php script) into a blob format so that I can store it as BLOB in mysql.
Thank you in advance.

Categories

Resources