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. Displaying the Image in the screen center using Canvas

I am using the canvas to display an image in Android. I want an image
to occupy the center of the screen irrespective of the sizes of the
screen. So how could i achieve it.

This is the snippet that i tried with, Please let me know your ideas
too.

@Override
        protected void onDraw(Canvas canvas) {
        ..........
            ..........
             // This moves the image to right end of the view.
            sampleImage.draw(canvas,getWidth(),getHeight());
            // Tried with this code too, but not getting the image at
the center of the screen.
            sampleImage.draw(canvas,getWidth()/2 - imageWidth/
2 ,getHeight()/2 - imageHeight/2);

            ..........
            ..........


Thank you


-- 

2. Create an Android Layer to support application to run on all the versions

Hello EveryOne,

Sorry for this question. I don't know this is correct question to ask.

Is it possible to create an android layer to support for running an
application on the any versions of android.

Presently we can't run Android 2.2 application on Android 1.5. So is
it possible to develop this.??

So what all the things i need to follow to achieve this?

Regards
AG

-- 

3. Help to get the latest version of monkeyrunner

4. Does ResultReceiver work across packages?

5. "MiB" in "Create new Android Virtual Device"

6. onCreate() vs class variable initializers

7. How to get this information (In this picture) to TextView on Android ?