uploading image + optional tags via HTTP POST

by Amit » Fri, 30 Apr 2010 13:42:00 GMT


Sponsored Links
 i,

I am a newbie to Android development; and I want to upload an image
(NOT an image file on the device, but a frame from a camera preview)
to a web-server. I searched around and got a snippet of code that can
be used to upload a byte array. This is the code:

public void uploadImage(final byte[] data) {
// spawn off a new thread to do the image uploading
new Thread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
Log.d(TAG, "onRun:");
HttpURLConnection conn = null;
try {
serverURL = new URL(URL);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
Log.e(TAG, "Malformed URL!");
e.printStackTrace();
}
try {
// open up a connection with the server
conn = (HttpURLConnection)
serverURL.openConnection();

// set up the conection
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection",
"Keep-Alive");

// now transfer the byte-stream
DataOutputStream ostream = new
DataOutputStream(conn.getOutputStream());
ostream.write(data);
ostream.flush();
ostream.close();

} catch (IOException e) {
// TODO Auto-generated catch block
Log.e(TAG, "No response from server!");
e.printStackTrace();
} finally {
conn.disconnect();
}

// wait for server response
try {
BufferedReader reader = new
BufferedReader(
new
InputStreamReader(conn.getInputStream())
);
String response;
while ((response = reader.readLine())
!= null ) {
Log.d("server response: ",
response);
}
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e



Other Threads

1. How to add more words onto Binary Dictionary(main.dict)??

http://developer.android.com/reference/android/provider/UserDictionary.Words.html






-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

--~--~---------~--~----~------------~-------~--~----~

2. What does /system/bin/vold do?

Hi,
      What does the /system/bin/vold do?

Elvis
--~--~---------~--~----~------------~-------~--~----~
unsubscribe: android-porting+unsubscr...@googlegroups.com
website: 

3. Slide Transitions between activities - Disable in application on 1.5?

4. UNLOCK Screen lock programatically

5. Facebook application

6. XML+XPath on Android using dom4j and Jaxen

7. setBuiltInZoomControls and debugging problems