base64 string is not uploaded in server android? - android

I need to send the image (from gallery) to server in the format of base64 string. I get the path of the image and converted it to base64 string. but at server the image is not showing fully. only 10% of the image is showing at server side.Please any one help me how to convert the image to base64 string.
code:
filePath = cursor.getString(columnIndex);
Bitmap bitmapOrg = BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(CompressFormat.PNG, 100, bao);
byte [] ba = bao.toByteArray();
ba1 =Base64.encodeToString(ba, Base64.DEFAULT);
image size:460kb

try with this i hope it will work for you and send using httpPost method.
public static String BitmapToString(Bitmap bmp) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
String base64 = Base64.encodeToString(byteArray, Base64.DEFAULT);
return base64;
}
and server side.
Base64 base64=new Base64();
saveImage(base64.decode(profilePic),"yourName");
public void saveImage(byte[] bytes,String name){
String path="D:/"+name.hashCode()+".png";
try {
FileOutputStream f = new FileOutputStream(path);
f.write(bytes);
f.close();
} catch (Exception e) {
e.printStackTrace();
}
}

Related

Converting bitmap into Base64 not encoding correctly in android.. I not getting image back when decoding

This is my code and I tried lot of code varieties already...
public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes,Base64.DEFAULT);
return encodedImage;
}
You can try following code to encode and decode image:
//Compress img and save
private String encodeBitmapAndSave(Bitmap bitmap1) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap1.compress(Bitmap.CompressFormat.PNG, 100, baos);
return Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
}
//Decompress img
private Bitmap decodeFromFirebaseBase64(String image) throws IOException {
byte[] decodedByteArray = Base64.decode(image, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(decodedByteArray, 0, decodedByteArray.length);
}

How to convert image to string in Android?

It's my MySql DB
Its encoding:-
public String convertBitmapToString(Bitmap bmp) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); //compress to which format you want.
byte[] byte_arr = stream.toByteArray();
String imageStr = Base64.encodeToString(byte_arr, 1);
return imageStr;
}
This is decoding:-
String img=o.toString();
byte[] imageAsBytes = Base64.decode(img.getBytes(), Base64.DEFAULT);
imageView.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
Thank you.
It could be done by
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%
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
And for Decode
private void decodeBase64AndSetImage(String completeImageData, ImageView imageView) {
// Incase you're storing into aws or other places where we have extension stored in the starting.
String imageDataBytes = completeImageData.substring(completeImageData.indexOf(",")+1);
InputStream stream = new ByteArrayInputStream(Base64.decode(imageDataBytes.getBytes(), Base64.DEFAULT));
Bitmap bitmap = BitmapFactory.decodeStream(stream);
imageView.setImageBitmap(bitmap);
}

Camera base64 image cannot be converted to Bitmap

I have been having trouble converting the base64 String Image that is being sent to me by the backend.
So this is how the backend sends the data.
final ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ImageIO.write(img, formatName, Base64.getEncoder().wrap(os));
return os.toString(StandardCharsets.ISO_8859_1.name());
} catch (final IOException ioe) {
throw new UncheckedIOException(ioe);
}
And this is how we convert it.
final byte[] data = Base64.decode(base64, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(data, 0, data.length);
The result is always null. Any help is highly appreciated.
EDIT:
These are the back end codes.
#Override
public BufferedImage base64ToImage(String base64Image) throws IOException {
byte[] imageBytes = Base64.getDecoder().decode(base64Image);
BufferedImage img = ImageIO.read(new ByteArrayInputStream(imageBytes));
return img;
}
public static String imgToBase64String(final RenderedImage img, final String formatName) {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ImageIO.write(img, formatName, Base64.getEncoder().wrap(os));
return os.toString();
} catch (final IOException ioe) {
throw new UncheckedIOException(ioe);
}
}
And this is our converting tool before sending the Base64 Image String to the back end.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return Base64.encodeToString(baos.toByteArray(), Base64.NO_WRAP);
Check that ur base64 encoded string does not start with data:image/jpg;base64,. If it does then remove it. The Base64.decode won't be able to decode it in this case. u can remove it by using encodedString.substring(encodedString.indexOf(",") + 1);. Let me know if this solves ur problem.
Try this,
ImageView driverImage = (ImageView) driverReportView.findViewById(R.id.imgViewDriverImage);
try {
byte [] encodeByte = Base64.decode(driverImageString.replace("\'/", "/"), Base64.DEFAULT);
Bitmap bitmap= BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
if (bitmap != null)
driverImage.setImageBitmap(bitmap);
}catch (Exception ex){
Log.e(TAG,"Error to display driver info "+ex.toString());
}
Where
driverImageString is your image string

How to convert Bitmap to Base64 String

I am developing an android application which uploads Base64 String of image into web server. Therefore, I have used the following code to convert a Bitmap into Base64 String.
public String getEncoded64ImageStringFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteFormat = stream.toByteArray();
String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP);
return imgString;
}
For example, I need to convert this image.
After the conversion, I'm not getting the Base64 String as I expected.
iVBORw0KGgoAAAANSUhEUgAAAIcAAAB7CAYAAABet5NoAAAABHNCSVQICAgIfAhkiAAAB/FJREFUeJzt3U1a4lgXB/D/uVQps3IBNuoKZEg56fQKml6BqRU0NdDXITWrBwbtDhp3oDuA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18Bm2WJ9zDCMKZy+FX6LZsJX9MeRxKY8NXEYACGVg5P/ptl6fUwwjA6HADws3ZlMdxb5OtQdwKCve31JEkxPhxA7nohRvQwwjByzrHs8L49cKCqyH4vJNYzt3TLROXwZLwXYlQPI4xMVA5PhnshvawFA8hY5fDLUC/EyB5GGJmqHH4Z6YVkNhhAhiuHx9xeiNk9jDAyHw7AxF5I9oMB5CQcgDG9kNRP0NEps3OOZQb0QiZKsZWXYAA5CgcAnPS/P8wPdZMOSKaaW2HlKhxAKr2QoVOe5i4YQI7mHKsk0AvpOeVpPWvNrbByHQ4AGH++bBDjrxhWnekeRhi5DwcQSy8k98EAChIOQGcvJB89jDAKEw5ARy+kOMEAcni0ss4OvZAJQf1WpGAABascnojnhWTmzC3dClU5PBF6IcZcfZaGQlYOvzW9kMyduaVbISuH38rzQhK8tZLJCl85PL5eSCF6GGH8H2WNU29+inIcAAAAAElFTkSuQmCC
The real Base64 String it should be this (I use this website to convert)
iVBORw0KGgoAAAANSUhEUgAAAIcAAAB7CAYAAABet5NoAAAACXBIWXMAABcSAAAXEgFnn9JSAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAABjBJREFUeNrsnc9x2zwQxRE1YDeQsVOBdPXJSgVRKhBdQeyTr/Ixp6QDUx1IHUinXKUKIo8bkCtwsJ7l98E0IUEkQWKB92Y4zp+xEz3+9LC7IsFPr6+vqgs9X92f6y9jfYz4oN9fH/iWJ33sjGP1+c/PlRIu7cOIfbg0vDg78m1rfez1senSh08+4WAjMjZj2NKPJaMWdGiTdkKAmOgvxXEmxYfW4dBGXDIQdFx49p0MyrU5eYBAkA+zloGwack+LIKEwzBj2sO5oCVoFgIk2ocx+3At3YfGcHAtQWb8COANS+bctv0OEvDmqPIha1qbNIKD19K8g9iss9xkXdUk2geC4jZAH5bsw74zODgtCIpvAdeBL5wiuee0WLRYbPvyYVInRU6GgzuQRQfFZluaMyT7lsEINTVtetAezLzBoQ2hDuS3IEM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WPdboxPr8wXFEmAIzOk8NIEHqRmKa6q5VxeNDJYSQIvdAbtLrhgtFbchgJgmGZXfTGmfS5DWivcACQg2B4G24FvayUlhgy4FJhFlKIfBj1DUYQyWEkCKapHQ23xCSHkSCpD8vmIYERVHKUUoSq85SGZZ3PMEQlRylFyKi7RMC4CxGMYJPDSBAyLeZhWW8zDPFwMCBUhywia3WLWxOD3jE6eDgYkJhmIUHMMMTWHBU1CBk5UvJnIVspYIhJDiNBJM9CgpphRJMcRoJInYWspYEhLjlKKUJVvoRZyDzUVjWq5CilCBl+BzCQHIcShMwPcRYS9AwjCTgYEKpDQpqFiAcjGjgYkBBmIb1foIOao7oG6XsWUgy3VrF4Gg0cDMhO9XMTlajhVpJwMCBdz0KiBCOqmsNSh1BR6HMWsuYaYx+jf1HDwYDQY0F+efjRomcYgON/QOgkPgIMwGEDhOqQNmYhUcwwAMdHQJrOQpIBI8pu5UgnU3cWQjOMrymBkVxyGAlyynUhYq7cQnK0kyCus5Bg7j5DcvSTIrRUTC1gjGOdYSA53FKEWtLydSHL1MFAcrxPEIKEZiFJzDBc9E+AAQBQ99BNOprhRAAAAABJRU5ErkJggg==
What is the problem in my conversion ?
Thank you !
public static String encodeTobase64(Bitmap image) {
Bitmap immagex = image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immagex.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
return imageEncoded;
}
public static Bitmap decodeBase64(String input) {
byte[] decodedByte = Base64.decode(input, 0);
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
use the following snippet to convert bitmap to byte array:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
then to encode base64 from byte array use the following statement:
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
public String getBase64ImageString(Bitmap photo) {
String imgString;
if(photo != null) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
byte[] profileImage = outputStream.toByteArray();
imgString = Base64.encodeToString(profileImage,
Base64.NO_WRAP);
}else{
imgString = "";
}
return imgString;
}
Pass your Bitmap in the parameters you will get your image string.

Android: Encode & Decode base64

How to encode and decode any image from base64 format.
I donno anything about base64, just now I came to know that it saves image in String format. Please explain about base64 and how to use it in android coding.
Will it reduce the size of an image?
Thanks in advance...
To encode any file:
private String encodeFileToBase64(String filePath)
{
InputStream inputStream = new FileInputStream(filePath);//You can get an inputStream using any IO API
byte[] bytes;
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
while ((bytesRead = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
bytes = output.toByteArray();
return Base64.encodeToString(bytes, Base64.DEFAULT);
}
Decode:
byte[] data = Base64.decode(base64, Base64.DEFAULT);
Base64 allows you to represent binary data in ASCII format, You can use it for send/receive images to an endpoint
To encode/decode check this two methods:
public static String getBase64(Bitmap bitmap)
{
try{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
return Base64.encodeToString(byteArray, Base64.NO_WRAP);
}
catch(Exception e)
{
return null;
}
}
public static Bitmap getBitmap(String base64){
byte[] decodedString = Base64.decode(base64, Base64.NO_WRAP);
return BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
}

Categories

Resources