how to convert an image into base64 string in android? - java

Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.image);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
final String encodedImage = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
This is my code. It uploads the image onto the server. However, All I can see is a box. Where am I going wrong?

Make sure you convert back to bitmap at the End, ie Server side
1, convert your imageview to bitmap.
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
2, convert bitmap to base64 String and pass to server
public String getEncoded64ImageStringFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 70, stream);
byte[] byteFormat = stream.toByteArray();
// get the base 64 string
String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP);
return imgString;
}
3, At server side convert base64 to bitmap.
(this is java code, do it in your server side language)
byte[] decodedString = Base64.decode(Base64String.getBytes(), Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
set this bitmap to display image

convert your image to bitmap.
File imagefile = new File(Environment.getExternalStorageDirectory(),"/image/test.jpg" );
FileInputStream fis = null;
try {
fis = new FileInputStream(imagefile);
} catch (FileNotFoundException e) {
logger.error(Log.getStackTraceString(e));
e.printStackTrace();
}
Bitmap bm = BitmapFactory.decodeStream(fis);
Bitmap to Byte[]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 10 , baos);
byte[] img = baos.toByteArray();
Byte[] to String:
String s= Base64.encodeToString(img , Base64.DEFAULT)

Related

Convert .gif image to base64 to upload to server

I'm trying to encode an animated gif to base64, so I can upload it to server. I am already doing it with static images, but if I try to do it with GIF, it simply does not work. I get the image from Intent image picker and put it inside an ImageView.
Here is the code I use to encode images to base64:
Bitmap bitmap = ((BitmapDrawable) ((ImageView) nextChild).getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, baos);
byte[] imginbyte = baos.toByteArray();
value = Base64.encodeToString(imginbyte, Base64.DEFAULT);
try this :
byte[] imginbyte = Base64.decodeBase64(bitmap.getImageBase64());
new FileOutputStream("image2.gif");
write(imginbyte);
close();

How to convert bitmap to byte array and byte array to bitmap in android?

I convert bitmap to byte array and byte array to bitmap but when I have to show converted byte array in ImageView then it will show image with black corners not show in PNG format. I want to show image in PNG format, how can I do that??
Here is a code for bitmap to byte array conversion and byte array to bitmap:
Bitmap into byte array in PNG compressed format:
public byte[] convertBitmapToByteArray(Bitmap bitmap) {
ByteArrayOutputStream stream = null;
try {
stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
return stream.toByteArray();
}finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
Log.e(Helper.class.getSimpleName(), "ByteArrayOutputStream was not closed");
}
}
}
}
and convert byte array to bitmap as:
BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
Try this code
//For encoding toString
public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = android.util.Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
//For decoding
String str=encodedImage;
byte data[]= android.util.Base64.decode(str, android.util.Base64.DEFAULT);
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
use this code:
public String convertBitmapToString(Bitmap bmp){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream); //compress to which format you want.
byte[] byte_arr = stream.toByteArray();
String imageStr = Base64.encodeBytes(byte_arr);
return imageStr;
}

Xamarin android image URI to Byte array

i'm simply trying to upload image to server.
when i choose image from , i get URI to that image.
the question is how can i convert this URI to byte[] byte array?
no more no less. thats my question
this is what ive been trying.
i tried to rewrite this https://colinyeoh.wordpress.com/2012/05/18/android-convert-image-uri-to-byte-array/
to C#
public byte[] convertImageToByte(Android.Net.Uri uri)
{
byte[] data = null;
try
{
ContentResolver cr = this.ContentResolver;
var inputStream = cr.OpenInputStream(uri);
Bitmap bitmap = BitmapFactory.DecodeStream(inputStream);
var baos = new ByteArrayOutputStream();
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, baos);
data = baos.ToByteArray();
}
catch (FileNotFoundException e)
{
e.PrintStackTrace();
}
return data;
}
but the error...
Error CS1503: Argument `#3' cannot convert `Java.IO.ByteArrayOutputStream' expression to type `System.IO.Stream' (CS1503) (Foodle.Droid)
how to fix this? or new code to get image from gallery and convert that to byte array is fine.
help!
public byte[] convertImageToByte(Android.Net.Uri uri)
{
Stream stream = ContentResolver.OpenInputStream(uri);
byte[] byteArray;
using (var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
byteArray = memoryStream.ToArray();
}
return byteArray;
}

Image to base64

How can I convert all the images stored in one folder to base64 data? in the following code tFotos is the folder where all my images are stored.
ArrayList<String> mList = new ArrayList<String>();
Bitmap selectedImage = BitmapFactory.decodeFile( Environment.getExternalStorageDirectory()+"tFotos");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
selectedImage.compress(Bitmap.CompressFormat.JPEG, 100,
stream);
byte[] byteArray = stream.toByteArray();
String strBase64 = Base64.encodeToString(byteArray, 0);
mList.add(StrBase64);
#Ruben JG
You have to provide full path of image like sdcard/foldername/filename.PNG/.JPEG

encoded image in base64 back to image

so I am trying to take a string for an image I encoded using base64 and turn it back into an image I can use in an ImageView. The code to encode it is:
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), options);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
image_str = Base64.encodeToString(b, Base64.DEFAULT);
I'm assuming it would be convert image_str back to a byteArray then back to bitmap?
I am not very familiar with base64 functions so I figured I would ask here while I search, to get more done in the same amount of time.
Thank you in advance,
Tyler
EDIT: I did find this bit of code but the image does not show up and logcat says decode returned false:
byte[] imageBytes=Base64.decode(imageString,Base64.NO_WRAP);
InputStream in = new ByteArrayInputStream(imageBytes);
Bitmap b = BitmapFactory.decodeStream(in);
You'd first decode the Base64 encoded string to bytes:
byte[] decodedBytes = Base64.decode(image_str, Base64.DEFAULT);
Then convert the bytes back to a JPG:
Bitmap bm = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);

Categories

Resources