2

I need to upload multiple images to PHP server from Android application. Multiple means that user can upload just 1 picture, or 2, or 3 or even 5 images.

Images I need to send to server with parameter path[numberOfImage], like this:

reqEntity.addPart("path[0]", bab); 

With this code I can upload an image to server.

 File file1 = new File(selectedPath1); Bitmap bitmap = decodeFile(file1); ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.JPEG, 75, bos); byte[] data = bos.toByteArray(); try { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(urlString); ByteArrayBody bab = new ByteArrayBody(data, "test.jpg"); MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("path[0]", bab); post.setEntity(reqEntity); HttpResponse response = client.execute(post); resEntity = response.getEntity(); response_str = EntityUtils.toString(resEntity); } 
4
  • And what's the problem? Did you try putting it in a loop? Commented Apr 16, 2014 at 14:15
  • Yeha, but how I can put in loop when I need multiple files and multiple ByteArrayBody? Commented Apr 16, 2014 at 14:22
  • You'll have to upload your files one-by-one, so the server side may know what's the beggining of one file and the end of another. You just need to put the code you've written in a loop, save the images you want to send somewhere (an Array for instance), and make as much HTTP POST requests as files you have. Commented Apr 16, 2014 at 14:30
  • Can you provide some sample code and I will accept your answer? Commented Apr 16, 2014 at 14:33

1 Answer 1

3

You can simply put it in a loop. Assuming you have an array of files (in this example called myFiles), you would just do something like this. Bear in mind that it's important of each iteration to create a new object of everything, so this way you're making sure you're sending always a different and independent object.

int i = 0; String[] myFiles = { "C:\path1.jpg", "C:\path2.jpg" }; for (String selectedPath : myFiles) { File file = new File(selectedPath); Bitmap bitmap = decodeFile(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.JPEG, 75, bos); byte[] data = bos.toByteArray(); try { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(urlString); ByteArrayBody bab = new ByteArrayBody(data, "test.jpg"); MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("path[" + String.valueOf(i++) + "]", bab); post.setEntity(reqEntity); HttpResponse response = client.execute(post); resEntity = response.getEntity(); response_str = EntityUtils.toString(resEntity); } catch (...) { ... } } 
Sign up to request clarification or add additional context in comments.

7 Comments

I cannot test it right now, I just adapted your code to what it would be, if you still have doubts I'll have a check tonight.
i cannot be resolved to a variable, because String.valueOf(i++) isn't part of for loop.
In my example I declared i outside (above) the loop, and increment it in this line. It just converts the current i to its value and increments it, it should compile.
Hey, I need help again on this problem? Can you help me?
please will someone look here? :(
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.