468,114 Members | 2,056 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,114 developers. It's quick & easy.

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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. HttpClient client = new DefaultHttpClient();
  3. HttpPost post = new HttpPost(URL)
  4.  
  5. try
  6. {    
  7.         MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
  8.         entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
  9.         entityBuilder.addTextBody("a", "apple");
  10.         entityBuilder.addTextBody("b", "banana");
  11.         entityBuilder.addTextBody("c", "cake");
  12.  
  13.         for (String element : list) {
  14.         File bin = new File(element);
  15.         FileBody bin1 = new FileBody(bin);                    
  16.         entityBuilder.addPart("ImageEncoded",bin1 );
  17.         HttpEntity entity = entityBuilder.build();
  18.         post.setEntity(entity);
  19.         response = client.execute(post);        
  20.         }    
  21.         httpEntity = response.getEntity(); 
  22.         result = EntityUtils.toString(httpEntity);
  23. }
  24.  

PHP code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php 
  3.  
  4.     $target_path1 = "uploads/";
  5.     $a = $_POST['a'];
  6.     $b = $_POST['b'];
  7.     $c = $_POST['c'];   
  8.     $fname = $_FILES['ImageEncoded']['name'];
  9.  
  10.     $query1 = mysql_query("INSERT INTO demo VALUES('','$a','$b','$c','$fname')");
  11.  
  12.     $target_path1 = $target_path1 . basename( $_FILES['ImageEncoded']['name']);
  13.     if(move_uploaded_file($_FILES['ImageEncoded']['tmp_name'], $target_path1)) {
  14.         echo "Success";
  15.  
  16.     } else{
  17.         echo "Fail";
  18.     }
  19.  
  20. ?>
  21.  
  22.  
May 2 '14 #1
3 10374
There is no way, you can pass directory and tell android to upload all images from the directory by magic.
Jun 9 '14 #2
Just zip the content of the directory and upload it as single file.
Jun 10 '14 #3
avdeshyadav
1 Bit
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.com/questions/8940969/uploading-multiple-image-on-php-server-from-android

for more advance level android development tutorials and sample code you can
https://itinsidenews.com/android-app...pment-tutorial
5 Days Ago #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by =?Utf-8?B?UmFqYWdvcGFs?= | last post: by
2 posts views Thread by sarayu | last post: by
3 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.