Android Sending Data to PHP Server - android

I am creating a sign up form where i need to send the following data to the PHP server to create the user account
First Name
Last Name
Email
PAssword
Image
i am sending the first four via JSON. now i am wondering how to include the image and send it to the server.

for this use the multipartentity concept.
for reference see the below code
MultipartEntity req=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
ByteArrayBody bab = new ByteArrayBody(data, "icon.png");
req.addPart("image", bab);
httppost.setEntity(req);
in that req.addPart("image", bab); "image" is the xml code.u sholud collect it .

you can transfer byteStream of image by HttpConnection .
i followed this link for the same .

you should go for Base64 encoding to send Image to sever.
see this link..Binary Data in JSON String. Something better than Base64

First you need to decide what kind of image you want to send. Do you want to choose an image from sd-card or take a photo with camera?
Here is very good tutorial how to do it, which even includes explanation how to implement croping of the image.
Next step, you will need to upload this file. You can get good information about that from here.

Related

How can I upload user's profile picture to server when registering new user ?

I am developing an android app using XMPP (Openfire) . User can register new account from that app and they can set their profile picture in register form. I want to know how can I save that profile picture to Openfire server.
You can use the vCard method that is given for Smack 4.1. Load the user's vCard when they are editing their profile information. Then, allow them to upload their avatar. Once they save it, you convert the Bitmap to a byte array, which is then sent to save vCard. Here's an example:
// Let the user pick their avatar
Bitmap bitmap;
// Take the avatar and convert it into a byte array:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// 90 refers the the compression quality. For PNG, the quality is ignored
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
byte[] avatarByte = stream.toByteArray();
// Once you get the byte array from the image, set the byte array to the vCard avatar
vCard.setAvatar(avatarByte);
// Then you can save the vCard details
vCardManager.saveVCard(vCard);
Hope that helps

OutOfMemoryException while sending an image to a webserver

So, to send an image to a web server i need to encode it to base64 and later decode on the server side.
This is what i am following.
But i get the OutOfMemoryException, at this line of code:
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
And the image size of 1mb. So, any alternate way to do this?
I read this:
Sending images using Http Post
which recommends to use MultipartEntity. But where exactly can i find those libraries? because 1 of the links is broken.
Code:
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
try {
File imageFile = new File(selectedImagePath);
Bitmap bitmap = BitmapFactory.decodeFile(imageFile
.getAbsolutePath());
setImage.setImageBitmap(bitmap);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
base64code = Base64.encodeToString(b, Base64.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
}
Probably 1 way might be to reduce the quality. But if i set it to 20 or 30 and the image is already a low quality 1(taken from a low end device). I might get a bad quality image.
Doing something like check the size and reduce the quality depending on that. I don't think is a good idea.
Thank You
Upload images to server with MultipartEntity is one of the good option.
MultipartEntity libraries Downloads :
apache-mime4j-0.6.jar
httpmime-4.0.1.jar
Hope it helps you.
Thanks.

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.

The image is too small when I try to upload it using Facebook Graph API and Android

Simply put I need to capture image using camera and upload it to facebook via my android application. And I successfully did that. The problem is when the photo posted in facebook, it's just too small and in low resolution while the image I took is in high resolution.
I understand that: in order to upload to facebook, i need to convert the captured image which is in bitmap format into byte array. So i have method for that:
public static byte[] convertBitmapToByteArray(Bitmap bm){
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.PNG, 100, bos);
byte[] bitmapdata = bos.toByteArray();
return bitmapdata;
}
Then to upload the image to facebook, i have code below where byteData is byte array I converted from bitmap image using the method above.
parameters.putString("message", "Test");
parameters.putByteArray("source", byteData);
String facebookResponse = facebookInstance.request(albumId+"/photos",parameters,"POST");
return facebookResponse;
I am pretty sure the problem is my convertBitmapToByteArray method since the method is to compress the bitmap image and turn it into byte array, and this made my image into low resolution image. However I can't seem to find the way to upload the image without converting it into byte array first. Any solution for me?
Alright even this thread is old, i found out the answer. It's not the problem of CompressFormant.JPEG or CompressFormat.JPG. Simply put, intent in android isn't designed to carry big data like image from activity through activity. I need to save the image from intent to sd card first before able to pull it out from there. It's my solution.

How to send an image through HTTPPost?

i'm creating an activity which needs to upload an image to a webservice using their api.
I found that if i use UrlEncodedFormEntity and send the image data through that. the webservice doesn't receive that. ( at least it will not be able to read that .)
In fact if i add some vars to send with the image data ( like name of the file, filesize ) they can be read from the webservice but the image data still doesn't appear if i try to read it serverside.
Right now i'm using UrlEncodedFormEntity with BasicNameValuePair as container for my data.
May be it will help you.
I've used the same functionality, but the web service was developed by me. I've post the image using the following:
I get the particular Bitmap icon and compress it in byte array like this:
ByteArrayOutputStream out = new ByteArrayOutputStream(10240);
icon.compress(CompressFormat.PNG, 100, out);
Then create HttpPost and set the the entity.
httpPostInstance.setEntity(new ByteArrayEntity(out.toByteArray()));
Check the "Content-type" header. You must properly set it to whatever your service expect.

Categories

Resources