Sending multiple images to the server from android

4.1k views Asked by At

I am trying to post multiple values from android to server using multipart

What I have done - I am able to send one image(from android drawable) to server.

my present code:

try
        {
            Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher); 

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost("http://10.0.2.2:7002/Details/");
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            try{
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bitmapOrg.compress(CompressFormat.JPEG, 75, bos);
                byte[] data = bos.toByteArray();
                ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
                reqEntity.addPart("key", bab);
            }
            catch(Exception e){
                //Log.v("Exception in Image", ""+e);
                reqEntity.addPart("picture", new StringBody(""));
            }
            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);
            }
        }catch(Exception e){
            e.getStackTrace();
        }

how to send two images:: this is my proposed model

try
        {
                        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
                        Bitmap bitmapOrg2 = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher2);   

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new        HttpPost("http://10.0.2.2:7002/Details/");
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            try{
                ByteArrayOutputStream bos = new ByteArrayOutputStream();

                                bitmapOrg.compress(CompressFormat.JPEG, 75, bos);       
                                bitmapOrg2.compress(CompressFormat.JPEG, 75, bos);

                                byte[] data = bos.toByteArray();

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

                                reqEntity.addPart("key", bab);
                                reqEntity.addPart("key1", bab2);
            }
            catch(Exception e){
                //Log.v("Exception in Image", ""+e);
                reqEntity.addPart("picture", new StringBody(""));
            }
            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);
            }
        }catch(Exception e){
            e.getStackTrace();
        }

My Question :: Am i following the correct way to pass multiple images above or is there a better way to do this ?

2

There are 2 answers

1
Nabz On BEST ANSWER

Yes, there is a better way to upload or download multiple files. first of all you must define an AsyncTask, and in his doInBackground function you must write your code to upload/download the file.

And by that, first of all your view will not freeze while downloading/uploading. The multiple files will be lunched asynchronously. and you can call it as easy as the following:

new MyAsyncTask(fileUrl1).execute();
new MyAsyncTask(fileUrl2).execute();

This is a better and more manageable method in order to use HTTP requests.

Hope that I've helped you. Good Luck

0
carrasc0 On

My way to do that:

public String getStringImage(Bitmap bmp) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 40, baos);
        byte[] imageBytes = baos.toByteArray();
        return Base64.encodeToString(imageBytes, Base64.DEFAULT);
    }

and send the image as String

In Server (Using Slim Framework):

$image = $app->request()->post('img_p');

file_put_contents($my_path, base64_decode($image));

I'm not sure that be the better way, but it works.