How to upload multiple images with other data to server from android in one request?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yateeshr
    New Member
    • May 2014
    • 1

    How to upload multiple images with other data to server from android in one request?

    Hi,

    I want to upload multiple images selected from gallery and upload it to server along with other data.
    Other data will be inserted into database in server.
    Till now what i have done is firing http url in a for loop till number of images. All images are getting uploaded.

    But, problem is, so many times the data also have been inserted into table.
    What I want is, to insert data only once and upload number of images selected into a folder in server.

    JAVA code:

    Code:
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(URL)
    
    try
    {	
            MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
            entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    	    entityBuilder.addTextBody("a", "apple");
    	    entityBuilder.addTextBody("b", "banana");
    	    entityBuilder.addTextBody("c", "cake");
    
    	    for (String element : list) {
    		File bin = new File(element);
    		FileBody bin1 = new FileBody(bin);			        
    		entityBuilder.addPart("ImageEncoded",bin1 );
    		HttpEntity entity = entityBuilder.build();
    		post.setEntity(entity);
    		response = client.execute(post);		
            }	
            httpEntity = response.getEntity(); 
            result = EntityUtils.toString(httpEntity);
    }

    PHP code:

    Code:
    <?php 
    
        $target_path1 = "uploads/";
        $a = $_POST['a'];
        $b = $_POST['b'];
        $c = $_POST['c'];   
        $fname = $_FILES['ImageEncoded']['name'];
    
        $query1 = mysql_query("INSERT INTO demo VALUES('','$a','$b','$c','$fname')");
    
        $target_path1 = $target_path1 . basename( $_FILES['ImageEncoded']['name']);
        if(move_uploaded_file($_FILES['ImageEncoded']['tmp_name'], $target_path1)) {
            echo "Success";
    
        } else{
            echo "Fail";
        }
    
    ?>
  • meditation
    New Member
    • Jun 2014
    • 13

    #2
    There is no way, you can pass directory and tell android to upload all images from the directory by magic.

    Comment

    • rules engine
      New Member
      • Jun 2014
      • 15

      #3
      Just zip the content of the directory and upload it as single file.

      Comment

      • avdeshyadav
        New Member
        • Apr 2021
        • 1

        #4
        When I started learning android development at one point, I needed to upload multiple images to a server that time, I faced this same problem. What helps me is I followed this question on stake overflow. I hope this
        will help you.
        https://stackoverflow.c om/questions/8940969/uploading-multiple-image-on-php-server-from-android

        for more advance level android development tutorials and sample code you can

        Comment

        • madankarmukta
          Contributor
          • Apr 2008
          • 308

          #5
          Zipping wont work.

          Could you please tell me why and where is your get request.

          Comment

          Working...