I want to know what is the best way to send an image to a web server.
For the moment, my server got php running on, and it's on linux. I got a fairly good access to the server, so i can implement other web services.
I'm not sure what is the easiest way to send a picture Assynchronously, and if possible with an icon on the status bar (for example, when we upload a file to facebook server)
Anyone can redirect me to a tutorial and / or example?
Thanx a lot.
For the moment, I use php to do some request, but, if I send an image synchrounously, this cause a big problem cuz my application will froze until the app is finished uploading it.
This is the code i'm "trying to use" but unsuccesfully (only tryed on the virtual machine though)
- Code: Select all
public void sendPicture(String picturePath)
{
DataOutputStream outputStream = null;
String pathToOurFile = picturePath;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
try
{
String data = "";
data = URLEncoder.encode("action", "UTF-8") + "=" + URLEncoder.encode("pictureUpdate", "UTF-8");
init("POST", "Content-Type", "multipart/form-data;boundary="+boundary);
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );
outputStream = new DataOutputStream( urlConn.getOutputStream() );
outputStream.writeChars(data);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
outputStream.writeBytes(data);
// Responses from the server (code and message)
receiveData();
Log.v(LOG_TAG, result);
fileInputStream.close();
outputStream.flush();
outputStream.close();
}
catch (Exception ex)
{
Log.e(LOG_TAG, ex.toString());
//Exception handling
}
}