Posting jpeg file as text in Android - android

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();

Related

Cons and pros of using Base64 encode and decode image in Android

This is block of code I used for convert from Image into Base64:
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.image);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte [] ba = bao.toByteArray();
String imgStr =Base64.encodeToString(ba,Base64.DEFAULT);
But I know we also can convert image into Binary data.
So just want to know the cons and pros of using Base64 encode image in Android. Should I use Base64 or Binary data to transfer image through message.
You should not send images through gcm server. It was not designed for that at all. You should be sending links, and other information, which is then used to get the data from a server after being received on the client (server-> client pushes).
Base64 is often used when transferring binary data as a string, which is stupid in this case because it increases the size of the data needing transfered, and you could easily do a POST to a server with the binary data directly.

adding a image from cam to a dynamically made html string

I want to add a image that i take from phones camera
and add it to a html that is build dynamically from user input.
so i cant get the html file from assets.
all the question/answers i found are for local images and html from assets.
Android - local image in webview
Android Development: Using Image From Assets In A WebView's HTML
any suggestions?
Thanks
One of the answers in the links you showed, suggests to use Base64 embedded image:
(https://stackoverflow.com/a/9000691/1271435)
<img src="data:image/jpg;base64,xxxxxxxxxxxxxxxxxxxxxxxxxxxx"/>
xxxxx = base64 encoded string of images bytes
This sounds like a good idea, and one way to do it is something like this:
After taking the image with camera you should get back an imageUri of the photo.
then:
1.Get the input stream of that image:
InputStream stream = getContentResolver().openInputStream(imageUri);
2.Fill in the stream into a ByteArrayOutputStream:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
fillStream(stream, baos); // dummy method, provide implementation
3.Convert the byte array to Base64:
String base64Image = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
Then you can use the base64 string as the data for your image in HTML.

How to read base64 encoded picture from rails by android

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})

Convert Base64 encoded String into Image file back on Java Server

I want to send Image from Android to Server. I decoded image into Base64 String and send it to the server. I use following code to convert Image to String
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte [] byteArray = bao.toByteArray();
String imageToString=Base64.encodeToString(byteArray,Base64.DEFAULT);
return imageToString;
Now i am unable to convert it back to Image on server side. I tried this
byte[] imageBytes=Base64.decode(imageString);
InputStream in = new ByteArrayInputStream(imageBytes);
BufferedImage bImageFromConvert = ImageIO.read(in);
ImageIO.write(bImageFromConvert, "jpg", new File("D:\\myImage.jpg"));
i am getting Bogus Huffman table definition exception and sometime im = null exception. plz tell me what mistake i am making
Edit: Error Message javax.imageio.IIOException: Bogus Huffman table definition at this line
BufferedImage bImageFromConvert = ImageIO.read(in);
Try this
byte[] imageBytes=Base64.decode(imageString,Base64.NO_WRAP);
InputStream in = new ByteArrayInputStream(imageBytes);
Bitmap b = BitmapFactory.decodeStream(in);
Well there might be multiple issues here. The first one I think is the fact that you convert the image bytes to String (encoding them with whatever default encoding you Android environment has) and the decoding that String back to bytes without ensuring that you use the same text encoding (and thus get the same bytes).
Why not send the bytes directly? Or better yet just upload the file directly via HTTP multi-part form. There's a tutorial on this here:
http://flo.dauran.com/194-android-uploader-une-image-sur-une-serveur-web/
(it's in french, but there's detailed code examples)

Send byte array of a Bitmap using http post in android?

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.

Categories

Resources