简体   繁体   中英

Sending photo from Android application to server side

I'm about to write server side aplication(most probably it would be PHP but JAVA is also possible) and android client side aplication. I try to figure out what is the best way to send photo from android aplication to server and receive it in server side. And if this any way to optimize/serialize sending more than one picture at a time?
Please provide me some reference or hint.
Thanks in advance.

U can use HTTP post for this. get ByteArrayOutputStream and compress JPEG image and use ByteArrayBody and post it using HttpClient

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        bm.compress(CompressFormat.JPEG, 75, bos);

        byte[] data = bos.toByteArray();

        HttpClient httpClient = new DefaultHttpClient();

        HttpPost postRequest = new HttpPost(

                "http://10.0.2.2/cfc/iphoneWebservice.cfc?returnformat=json&method=testUpload");

        ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");

        // File file= new File("/mnt/sdcard/forest.png");

        // FileBody bin = new FileBody(file);

        MultipartEntity reqEntity = new MultipartEntity(

                HttpMultipartMode.BROWSER_COMPATIBLE);

        reqEntity.addPart("uploaded", bab);

        reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf"));

        postRequest.setEntity(reqEntity);

        HttpResponse response = httpClient.execute(postRequest);

        BufferedReader reader = new BufferedReader(new InputStreamReader(

                response.getEntity().getContent(), "UTF-8"));

        String sResponse;

        StringBuilder s = new StringBuilder();



        while ((sResponse = reader.readLine()) != null) {

            s = s.append(sResponse);

        }

You can find related code here. http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM