android send binary data in http - android

I have the following code to send an image (bitmap binary data) using android http default client:
//gets raw binary data and convert it to a string
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String imageData = new String(imageBytes);
//http put body
StringEntity se = new StringEntity(imageData);
putRequest.setEntity(se);
I need to send its raw binary content in the put request body, but it seems that the content is not being properly sent.
I can send an image binary using curl with the --data-binary (that's what I am trying to do in android).
Is the process in converting the bitmap binary to string correct?
Thanks!

Well, my bad.
In case anyone makes the same mistake:
I should have used the ByteArrayEntity class instead of the StringEntity one:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
//http put body
ByteArrayEntity be = new ByteArrayEntity(imageBytes);
putRequest.setEntity(be);
I was trying to convert the bytearray into a string and send it, the correct way to do it is to use the ByteArrayEntity class.

Related

how to upload image as byte array to Rest web-service using http post method in android

I created app which can upload the images to the web-service. In that, how can I upload use the image as byte[] array while uploading image using HTTP post method to rest web-service.
How to pass the byte[] array to web-service along with file name and file length?
Drawable d = context.getResources().getDrawable(R.drawable.human_head);
Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
byte[] bitmapdata = stream.toByteArray();
entity.addPart("image", new ByteArrayBody(bitmapdata,".png"));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost, WebService.getInstance(Constants.WEB_SERVER).getLocalContext());

Can we send image as a string parameter in webserrvices in android

I need to send image as a string parameter in webservices to server. Just like we send string parameter in webservices. For this I am converting image into string as follows
Resources r = this.getResources();
Bitmap bm = BitmapFactory.decodeResource(r, R.drawable.icon);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 50, baos); //bm is the bitmap object
byte[] image = baos.toByteArray();
String encodedImage = Base64.encodeToString(image,Base64.DEFAULT);
Now I need to send this string encodedImage like this.
calling Url-- http://pdtrptr/asfsdf/services/add.php?file=encodedImage
My question is how can we send this string image along with url --like above url
Is server coding for receiving string image differs for android compare to iphone because (after sending the image from android to server, when trying to get from server i am getting null value), where as iphone mobile is able to send and receive the image.
Thanks
We can try the code which is providing in following link...
Image Upload with HttpPost

How to convert jpeg image to base64 encoded binary value?

In my android application, i want to convert a jpeg image to base64 binary encoded value.
How to acheive this in android ?
Edit
I have encoded my bitmap image into base64 string by following code:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
byte[] val = stream.toByteArray();
String ba = Base64.encodeToString(val, Base64.DEFAULT);
The output is in the form of string. BUt I need the output as binary value.
if using KSOAP2 for connectivity use this
new MarshalBase64().register(envelope); this is do the magic.

Dynamically conversion of image to binary and vice versa

How can I convert image to binary data..???
I want to send that converted binary data to
another device or to the web server.
Which mechanism is best to do this.?
Image is in Bitmap then use the following code to convert that image to binary. By using following code
Bitmap photo;// this is your image.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
To get Image From Binary use the following sample:
Bitmap bMap = null;
bMap = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
I found a good example for uploading the image to the server.
create a bitmap variable before do anything.
variable to set a name to the image into SD card.
this variable, you have to put the path for the File, It's up to you.
sendData is the function name, to call it, you can use something like
sendData(null).
remember to wrap it into a try catch.
private Bitmap bitmap;
public static String exsistingFileName = "";
public void sendData(String[] args) throws Exception {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
// here, change it to your php;
HttpPost httpPost = new HttpPost("http://www.myURL.com/myPHP.php");
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
bitmap = BitmapFactory.decodeFile(exsistingFileName);
// you can change the format of you image compressed for what do you want;
// now it is set up to 640 x 480;
Bitmap bmpCompressed = Bitmap.createScaledBitmap(bitmap, 640, 480, true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// CompressFormat set up to JPG, you can change to PNG or whatever you want;
bmpCompressed.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
// sending a String param;
entity.addPart("myParam", new StringBody("my value"));
// sending a Image;
// note here, that you can send more than one image, just add another param, same rule to the String;
entity.addPart("myImage", new ByteArrayBody(data, "temp.jpg"));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost, localContext);
BufferedReader reader = new BufferedReader(new InputStreamReader( response.getEntity().getContent(), "UTF-8"));
String sResponse = reader.readLine();
} catch (Exception e) {
Log.v("myApp", "Some error came up");
}
}
Try this
Let img contains Bitmap image
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "png", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
imageInByte now contains bytedata of bitmap image.
For converting reverse
Bitmap bp = BitmapFactory.decodeByteArray(imgArray, 0,imgArray.length);
Hope this may help you
if you want to send to webserver use HttpPost request using HttpClient

android convert bitmap to bufferedinputstream

Newb question;
I've got a bitmap in memory;
Private Bitmap MyPicture;
Then later, I fill that MyPicture from imager from the camera. I need to upload that photo using the FTP client from the apache commons.
fcon.storeFile("filename", new BufferedInputStream(MyPicture.????));
But the apache want a BufferedInputStream. How do I convert the memory Bitmap to a memory stream?
Thanks guys!
This is what I was looking for;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
si.Image.compress(CompressFormat.JPEG, 100, stream);
InputStream is = new ByteArrayInputStream(stream.toByteArray());
The last line was the missing link...
Converting the bitmap to a byte array is as easy as:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MyPicture.compress(Bitmap.CompressFormat.PNG, 100, baos);
baos.toByteArray();
And then you can create a BufferedInputStreamto write the bytes to your file.

Categories

Resources