12-16-2012 11:32 PM
i want to edit the user account in which along with firstname,lastname i am getting an image url. Through the url i am downloading the image. Next i am changing the image by browsing my device folder. This change now i want to send to server. The server is accepting the image in the form of file and the remaining in String. Please tell me how to convet the image to file and send to server along with Strings . Its urgent .Please help me
12-17-2012 03:48 AM
12-17-2012 03:55 AM
I tried a lot. Actually i loading the image from server ,convert it to bytes and then uploading that byte date to server. As in this form only i am getting the code from serching. but on the server side the image has to identified as file.Because of that it is throwing error.wat to do. This is the code where i am downloading from server:
now the "buffer" i am sending through multipart posting
try {
HttpConnection httpConnection = (HttpConnection) Connector.open(barcodeurl);
rc = httpConnection.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
}
InputStream httpInput = httpConnection.openInputStream();
InputStream inp = getClass().getResourceAsStream("Mystuff/accountset
is = inp;
data = IOUtilities.streamToBytes(inp);
}catch (Exception e) {
// TODO: handle exception
}
EncodedImage image = EncodedImage.createEncodedImage(data,0,data.length
Bitmap bitmapScaled = new Bitmap(75,75);
Bitmap b = image.getBitmap();
// byte[] buffer = IOUtilities.streamToBytes(is);
// is.read(buffer);
is = b.getClass().getResourceAsStream("Mystuff/accounts
byte[] buffer = new byte[is.available()];
is.read(buffer);
12-17-2012 03:56 AM
Sorry that above code is wrong....
This the right one:
int rc;
String barcodeurl=accountDetail.userProfilePicture;
barcodeurl +=GetServerDetail.getConnectionString();
try {
HttpConnection httpConnection = (HttpConnection) Connector.open(barcodeurl);
rc = httpConnection.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
}
InputStream httpInput = httpConnection.openInputStream();
InputStream inp = httpInput;
is = inp;
data = IOUtilities.streamToBytes(inp);
}catch (Exception e) {
// TODO: handle exception
}
byte[] buffer = IOUtilities.streamToBytes(is);
12-17-2012 04:37 AM
12-17-2012 04:44 AM
Ya i know. After this above code i am using "buffer" value to post the data as follows:
public byte[] getBody() {
ByteVector buff = new ByteVector();
for (int i = 0; i < parameters.size(); i++) {
RequestParam param = (RequestParam) parameters.elementAt(i);
if (param instanceof BinaryRequestParam)
{
appendArray(buff, getBinaryHeader((BinaryRequestParam)param));
appendArray(buff, ((BinaryRequestParam)param).getData());
appendArray(buff, CRLF.getBytes());
} else {
appendArray(buff, postParameter(param.getName(), param.getValue()));
}
}
appendArray(buff, closeBoundary().getBytes());
return buff.getArray();
}
//for string values//
==============
private static String postParameterAsString(String name, String value) {
String result = boundary() + CRLF;
result = result + "Content-Disposition: form-data; name=\"" + name + "\"" + CRLF + CRLF;
result = result + value + CRLF;
return result;
}
//for image//
==========
private byte[] getBinaryHeader(BinaryRequestParam param) {
String header = boundary() + CRLF;
header += "Content-Disposition: form-data; filename=\"" + param.getName() + "\"" + CRLF+ CRLF;
header+= ((BinaryRequestParam)param).getData()+CRLF;
// header += "Content-Type: "+ param.getContentType() + CRLF + CRLF;
return header.getBytes();
}
//for sending image and data//
=========================
try {
String urlToSend = url;
connection = (HttpConnection)Connector.open(urlToSend, Connector.READ_WRITE, true);
connection.setRequestMethod(HttpConnection.POST);
connection.setRequestProperty("Content-Type", MULTIPART_CONTENT_TYPE);
connection.setRequestProperty("Content-Length", String.valueOf(bytes.length));
outputStream = connection.openOutputStream();
int offset = 0;
int chunk = DATA_CHUNK_SIZE;
int len = bytes.length;
while (offset < len) {
if (offset + chunk >= len) {
chunk = len - offset;
}
outputStream.write(bytes, offset, chunk);
offset += chunk;
}
return getResponseAsSting(connection);
} finally {
safelyCloseStream(outputStream);
safelyCloseStream(connection);
if (bytes != null) {
bytes = null; // notify VM it can safely free the RAM
}
}
//for getting the result//
===================
private static String getResponseAsSting(HttpConnection conn) throws IOException {
String result = "";
InputStream inputStream = null;
try {
inputStream = conn.openInputStream();
int len;
byte[] data = new byte[512];
do {
len = inputStream.read(data);
if(len > 0){
result += new String(data, 0, len);
}
} while(len > 0);
} catch (Exception e) {
if (e instanceof IOException) {
throw (IOException)e;
}
} finally {
safelyCloseStream(inputStream);
}
return result;
}
12-17-2012 04:52 AM
I am not clear on what your problem is. Can you please explain what you are asking, in some other way - perhaps you could describe the steps your program is going through and the problem you are having at each step. For example, are you have a problem opening the connection or reading the data or what?
It will be useful I think to get the process 100% clear in your own head too. I suspect you may be mixing up sending data in bytes and sending data as text (you call it a String). If you are sending data as text, then it is converted to bytes and so you need to consider what encoding method is used to convert the characters to bytes. If you didn't understand this, then I suspect the process will cause you problems.
Here are some general points that might help:
a) If you have to send the data to the Server as a String, then you need to find out what form this Sting needs to take. The Server will specify that. So check the requirements of the Server you are sending it to.
b) Your code in the last Post is faulty. You attempt to read from the same input stream twice, is and inp are the same. You can't do that - just create a copy of the byte array.
c) I hope the code we see is just a cut down version. There appears to be me to be a load of error checking and tidying up of open streams that would normally be part of this process that you have not included.
d) I presume that the Server you are sending the image to is not the same Server you are getting the image from.