I wonder how you should do if you are going to convert an String that contains an array of bytes, actually bytes from an image into an byte array.
I guess this is a pretty common issue so i guess someone could give any suggestions.
If it is better to convert an JSONArray into an byte array i would like to know why.
I wonder how you should do if you are going to convert an String that contains an array of bytes, actually bytes from an image into an byte array.
It depends on how you've constructed the string in the first place. If something somewhere has used
new String(imageData)
then you've already lost information, potentially. Image data isn't text.
To propagate opaque binary data in text, you should use base64 (or potentially hex). Android already has a Base64 class that makes this easy:
String text = Base64.encodeToString(originalBytes, Base64.DEFAULT);
...
byte[] roundTrip = Base64.decode(text, Base64.DEFAULT);
EDIT: If you can't use that class, this public domain base64 encoder should work fine.
Byte[] to String
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Your_Bitmap.compress(CompressFormat.PNG, 0 , baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
String bytetostring = Base64.encodeToString(b, Base64.DEFAULT);
return bytetostring ;
String to Byte[]
byte[] data = Base64.decode("Your string", Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data.length);
Related
I want to convert my app images to base64 so it won't show on the gallery. I have tried various techniques. The image is from a zip file so it is a byte array at that point. The length of the base64 change when I change the byte array size. What is the proper byte array size? And the base64 encoded image doesn't work too.
The primary code is
String encodedImage = Base64.encodeToString(buffer, Base64.DEFAULT);
buffer is the byte array(102400)
and it contains the image too.
The image is a 7KB file and the output is 400KB
You may try following function to convert image into Base64:
public void toStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
}
Above function takes Bitmap image and converts it into Base64 encoded string. This is working in my project and I hope this will help you too.
I have an image that I encode using this code :
Bitmap bm = BitmapFactory.decodeFile(selectedImagePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
It encodes well. After that I'm getting the String in a file in my computer and I try to decode it with this website :
http://www.askapache.com/online-tools/base64-image-converter/
And I'm getting a null picture. However, when I decode it directly in my app and display it like :
public static Bitmap decodeBase64(String input) {
byte[] decodedByte = Base64.decode(input, 0);
return BitmapFactory
.decodeByteArray(decodedByte, 0, decodedByte.length);
}
private ImageView img;
img.setImageBitmap(decodeBase64(encodedImage));
I'm getting the right picture. So I was wondering if i actually can't decode an android encoded image with an other decoder than android's one or my file is corrupted meaning that I have missing characters. However even if I don't have the whole encoded file, the decoded picture i'm getting with the website should look like to the good one right ?
Thank you in advance
In my C# application, I am having a Bitmap encoded to a base64 string sent over from an android application, then I decode it and set it equal to a byte array.
I get this exception though:
"A first chance exception of type 'System.FormatException' occurred in
mscorlib.dll".
Android Side:
byte[] iconByteArray = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, baos); //Bitmap bitmap created elsewhere
iconByteArray = baos.toByteArray();
encodedIcon = Base64.encodeToString(iconByteArray, Base64.DEFAULT);
return encodedIcon;
C# Side:
byte[] arr = System.Convert.FromBase64String(encodedIcon); //this throws that exception
Does anyone know of the cause? I'm guessing the format of the Base64 string in Java is not able to be decoded so simply by the C# application? It looks like I may have to do something like this and replace some characters? I tried this solution and got the same exception.
The string sent through Android looks like this
"/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsK\nCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQU\nFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCABgAGADASIA\nAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA\nAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3\nODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm\np6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA\nAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx\nBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK\nU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3\nuLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD8qq2t\nG8EeIvENqbrStC1PUrYMUM1pZyyoGHUZVSM8jj3rFr9av2Ipl+Gf7PHh+xn1qfzNQL6rttY5AsYm\nCsEPzDJAHJx3roo4atiW1QjzNHBi8fhcBFSxU+VPRaN/kmfl9/wqrxp/0KWu/wDgsn/+Io/4VV40\n/wChS13/AMFk/wD8RX7cj4m"
Try
encodedIcon = encodedIcon.Replace(#"\n", "");
if(encodedIcon.Length % 4 != 0)
// we may have 0, 1 or 2 padding '='
encodedIcon += new string('=', 4 - encodedIcon.Length % 4);
byte[] arr = System.Convert.FromBase64String(encodedIcon);
According to Base64:
After encoding the non-padded data, if two octets of the 24-bit buffer are padded-zeros, two "=" characters are appended to the output; if one octet of the 24-bit buffer is filled with padded-zeros, one "=" character is appended. This signals the decoder that the zero bits added due to padding should be excluded from the reconstructed data. This also guarantees that the encoded output length is a multiple of 4 bytes.
In this line
encodedIcon = Base64.encodeToString(iconByteArray, Base64.DEFAULT);
you can change Base64.DEFAULT to Base64.NOWRAP
It removing \n from the encoded string.
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);
I have this custom adapter for my listview that is getting filled with a ArrayList of a custom rowItem class where I put data in.
rowItem.getImage() will return this byte array:
I have put the long string under this in a byte[] variable with getBytes()
R0lGODlhZAAvAPcAAAAAAAAAMwAAZgAAmQAAzAAA/wArAAArMwArZgArmQArzAAr/wBVAABVMwBVZgBVmQBVzABV/wCAAACAMwCAZgCAmQCAzACA/wCqAACqMwCqZgCqmQCqzACq/wDVAADVMwDVZgDVmQDVzADV/wD/AAD/MwD/ZgD/mQD/zAD//zMAADMAMzMAZjMAmTMAzDMA/zMrADMrMzMrZjMrmTMrzDMr/zNVADNVMzNVZjNVmTNVzDNV/zOAADOAMzOAZjOAmTOAzDOA/zOqADOqMzOqZjOqmTOqzDOq/zPVADPVMzPVZjPVmTPVzDPV/zP/ADP/MzP/ZjP/mTP/zDP//2YAAGYAM2YAZmYAmWYAzGYA/2YrAGYrM2YrZmYrmWYrzGYr/2ZVAGZVM2ZVZmZVmWZVzGZV/2aAA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DCdUgU5AIr3pLuMbzmQTIEuHAZT8IfNhqsqLCx+yIQ5Me2ZXS1i3yiqcgAQEAOwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==
Now I have this code:
byte[] imageData = rowItem.getImage();
Drawable logoDrawable = null;
if (imageData != null) {
Bitmap logoBitmap = BitmapFactory.decodeByteArray(imageData, 0,
imageData.length);
logoDrawable = new BitmapDrawable(context.getResources(), logoBitmap);
}
holder.imageView.setImageDrawable(logoDrawable);
But it won't display a image, if I set a image out of my drawable map it will show so it reconizes the imageview.
What you've shown isn't a byte array - it's text, which looks very much like base64 data.
You should try decoding it from base64 to "raw" bytes first:
byte[] imageData = Base64.decode(rowItem.getImage(), Base64.DEFAULT);
... or fix wherever you're getting rowItem from to perform the base64 decoding for you.
The data looks like base64 encoded binary data. Base64.decode() it first and then pass the decoded data to BitmapFactory.