Hi um using both of these options one at a time to encode an image and send it over http post in android
String encodedImage = Base64.encodeToString(imageData, Base64.URL_SAFE + Base64.NO_WRAP);
String encodedImage = Base64.encodeToString(
imageData,Base64.DEFAULT);
It's convert the image in to encoded string sends it over http post.
In WCF side um trying to decode the image as follows and the encoded image string comes as in encoded format.
byte[] contents = Convert.FromBase64String(encodedImage.Trim());
After this it throws an exception saying
The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters.
what can possibly go wrong . I have a no clue of this. I will be thankful if anyone can guide me.
Related
I have a photo (jpeg) format in an Android device. Instead of posting the file as a file using HTTP I prefer to convert that to a string and post it as string using http to a spreadsheet. I understand jpeg files are encoded and opening them as string shows funny characters. My question is if I send these characters as string using http, can I get them back on the other side using a binary file editor and save them as jpeg?
One thing you can do is, on client side:
1.Convert image to byte array.
Path path = yourImageFile.toPath();
byte[] byteArray = Files.readAllBytes(path);
2.Encode the byteArray to Base64 String.
String encodedString = Base64.encodeBase64URLSafeString(byteArray);
3.That's it, send via http.
I'm not really sure what you mean by a binary file editor, but from server side you can retrieve the image like this (if using Java):
byte[] decodedByte = decodedBase64.decodeBase64(encodedString);
FileOutputStream out = new FileOutputStream("newPathToYourImage");
out.write(decodedByte);
out.close();
Sorry for asking this question twice times, but I did research 2 hours...
I have problem with Base64 decode String in my Android.
For example:
encoded string: VgFzJ1+TrFa7WsXS5w==
The results in javascript and PHP: Vs'_¬V»ZÅÒç
The result in my Android: Vs'_��V�Z���
I found a solution from this page Android PHP Base64 decode with different results
They say
By this You convert the input String content to ISO-8859-1 encoded byte stream that will be decoded from base64.
This is my function decoder based on what they say:
byte[] b = Base64.decode(data.getBytes("ISO-8859-1"), Base64.DEFAULT);
return new String(b, "UTF-8");// event if remove UTF8
But still got the same error
Sorry, I have resolved by myself
It's very simple to get string from ISO-8859-1
return new String(b, "ISO-8859-1")
My Android app needs to connect to a webservice that will decode a unicode string using utf8_decode. How can I encode my string in my application in a similar way as php utf8_encode?
I have found CharsetEncoder but am not sure how to use it.
Thanks for your advice!
You can do this using just the String class, but just a note about converting text to/from UTF and ISO. When decoding from UTF-8 to ISO-8859-1 will cause "replacement characters" (�) to appear in your text when unsupported characters are found.
To encode texts:
byte[] utf8 = new String(latin1, "ISO-8859-1").getBytes("UTF-8");
or
byte[] latin1 = new String(utf8, "UTF-8").getBytes("ISO-8859-1");
I want to send an base64 encoded picture from ruby on rails to android.
Sending the data to rails included the picture encoding and decoding works, the data transfer from rails back to android works also, but I can't decode the picture, which was encoded by rails. I always get a NullpointerException at the following marked line:
imageOutFile = new FileOutputStream(file);
byte[] bytes = Base64.decode(imageString, Base64.URL_SAFE);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
**bitmap.compress(Bitmap.CompressFormat.JPEG, 100, imageOutFile);**
The encoded string looks like the following (it's the shortest one):
_9j_4AAQSkZJRgABAQEASABIAAD_4Se-RXhpZgAASUkqAAgAAAALAA4BAgAUAAAAkgAAAA8BAgAUAAAApgAAABABAgAJAAAAugAAABIBAwABAAAAAQAAAA==
Another string is this:
_9j_4AAQSkZJRgABAQEAAQABAAD_2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH_2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH_wAARCAHgAoADASIAAhEBAxEB_8QAHAABAQEBAQEBAQEAAAAAAAAAAAoICQcGAgED_8QARRAAAQIBCAYIBAQEBwACAwAAAAYHBQEECAkZOIe3AxQVFqfWAhhWV1iWl9cXaNXnERIjJRM2VfAkJic1UZSkUmchMjf_xAAXAQEBAQEAAAAAAAAAAAAAAAAACQEH_8QAMxEBAAAEAwUGBQMFAAAAAAAAAAEGB1YCGJYDcbHV1ggXQUam8AURFjFRYaG1IYGRxcb_2gAMAwEAAhEDEQA_AIPwAAAAAAACo6rguPM3iPm-vCXEqOq4LjzN4j5vrwDbQAAAAAAABLnWM33Hqw4yoQ5UYS51jN9x6sOMqEOBiMAAAAAAAAqOq4LjzN4j5vrwlxKjquC48zeI-b68A20AAAAAAAAS51jN9x6sOMqEOVGEudYzfcerDjKhDgYjAAAAAAAAKjquC48zeI-b68JcSo6rguPM3iPm-vANtAAAAAAAAEudYzfcerDjKhDlRhLnWM33Hqw4yoQ4GIwAAAAAAACo6rguPM3iPm-vCXEqOq4LjzN4j5vrwDbQAAAAAAABLnWM33Hqw4yoQ5UYS51jN9x6sOMqEOBiMAAAAAAAAqOq4LjzN4j5vrwlxKjquC48zeI-b68A20AAAAAAAAS51jN9x6sOMqEOVGEudYzfcerDjKhDgbYsbvmb4PfdIWN3zN8HvukdtABxLsbvmb4PfdIWN3zN8HvukdtABxLsbvmb4PfdIWN3zN8HvukdtABxLsbvmb4PfdId
In Logcat I get a message like this, after the failed decoding:
--- decoder->decode returned false
I encode the string with the following method and send the string with a json object to android. The picture was uploaded with the uploader of CarrierWave.
Base64.urlsafe_encode64(File.read("public#{picture_path.to_s}"))
I have to read the file binary ("rb") and then encode it.
Base64.encode64(File.open("public#{picture_path.to_s}", "rb") {|io| io.read})
In my application I need to call an API Using Http Post to send some information to the web server. So I need to append userid, name, phoneNo and image to the url. I need to convert the image to a byte array and append it to url. I used the following code but it didn't work for me. I just got a byte array like this [B#4055dd90
My code is
Bitmap bitmap1 = ((BitmapDrawable) d).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap1.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
Can Some one please help Me ?
Thanks in advance !
Two things:
Images can get pretty big. So, you may be better off using a JPG, as you can compress it (a PNG does not compress). So, you'd use Bitmap.CompressFormat.JPEG and set the amount of compression you want.
When sending in your post, you should encode the byte[] like this: Base64.encodeBase64String( bitmapdata);
Let me know if that works for you. OH, and don't forget to unencode it on your server side.
You can not send the images in the byte array form directly to server. You have to encode it into Base64 String with the help of Base64 Class. Encode Bitmap byte array to Base64 string and send that string to server with the help of HTTPPost method. If you have any doubt then you can comment.