3

How to upload an image and audio together in android? I was successful in uploading an image and audio in one request but how to add multiple files.

I referred this link Android:How to upload .mp3 file to http server? and it's perfectly working, but I want to add another file in this request.Please help me to do this. For that what change should I do in the android and php side.

Community
  • 1
  • 1
Kikki
  • 496
  • 1
  • 10
  • 17

1 Answers1

14

Just use the httpmime-4.0.jar and apache-mime4j-0.4.jar and set the entity as MultipartEntity. you can use as many file as you want.

Here is the stuff,

HttpPost httpost = new HttpPost("url for upload file");

MultipartEntity entity = new MultipartEntity();
entity.addPart("myIdentifier", new StringBody("somevalue"));
entity.addPart("myImageFile", new FileBody(imageFile));
entity.addPart("myAudioFile", new FileBody(audioFile));

httpost.setEntity(entity);
HttpResponse response;
response = httpclient.execute(httpost);

and for php side you can use these entity identifier names "myImageFile" and "myAudioFile" and move these files in appropriate folder.

user370305
  • 108,599
  • 23
  • 164
  • 151
  • if you find this answer is useful for you. then vote it, for other user. – user370305 Aug 12 '11 at 10:43
  • Where to use this "httpmime-4.0.jar and apache-mime4j-0.4.jar". You didn't mention about this in your answer. – Kikki Aug 14 '11 at 06:22
  • from where should I download these jars – Kikki Aug 14 '11 at 08:08
  • you can download apache-mime4j-0.5.jar from [here](http://www.java2s.com/Code/Jar/MNOPQR/Downloadapachemime4j05jar.htm) and httpmime-4.0.jar [here](http://www.findjar.com/jar/org/apache/httpcomponents/httpmime/4.0/httpmime-4.0.jar.html?all=true) and use in your project buildpath. – user370305 Aug 17 '11 at 11:47
  • voted up, nice guide, would some1 also let me know how I can use this using Async task, I want user to click upload button, then multiple files with data is to upload using same method above, but after the click the activity should be finish and I have to check if network available then upload data else I do have to wait for network in background, means implement broadcast reciever, would any1 getting the follow kindly update me the best procedure to follow, I am junior – Abdul Wahab Jul 25 '12 at 18:50
  • +1. It would be even better if the code showed where the httpclient came from. – Mahmoud Badri Jun 10 '13 at 23:29
  • Nice Answer , You deserve +1 – Miral Dhokiya Feb 13 '14 at 08:54