How To Upload Array Of Image Files Using Multipart In Android Volley Library
I'm just beginner in Android App development. I want to upload image files using Android Volley library. I have used this helper class found somewhere in some blog.: public cla
Solution 1:
Try this code:
I had similar kind of issue i fixed using this
Uploading multiple files in array
using multipart
Add dependecies
compile'org.apache.httpcomponents:httpcore:4.2.4'compile'org.apache.httpcomponents:httpmime:4.2'compile'com.mcxiaoke.volley:library:1.0.19'
2.Add in gradle
defaultConfig {
useLibrary 'org.apache.http.legacy'
}
Helper class AndroidMultiPartEntity.java
publicclassAndroidMultiPartEntityextendsMultipartEntity { privatefinal ProgressListener listener; publicAndroidMultiPartEntity(final ProgressListener listener) { super(); this.listener = listener; } publicAndroidMultiPartEntity(final HttpMultipartMode mode, final ProgressListener listener) { super(mode); this.listener = listener; } publicAndroidMultiPartEntity(HttpMultipartMode mode, final String boundary, final Charset charset, final ProgressListener listener) { super(mode, boundary, charset); this.listener = listener; } @OverridepublicvoidwriteTo(final OutputStream outstream)throws IOException { super.writeTo(newCountingOutputStream(outstream, this.listener)); } publicinterfaceProgressListener { voidtransferred(long num); } publicstaticclassCountingOutputStreamextendsFilterOutputStream { privatefinal ProgressListener listener; privatelong transferred; publicCountingOutputStream(final OutputStream out, final ProgressListener listener) { super(out); this.listener = listener; this.transferred = 0; } publicvoidwrite(byte[] b, int off, int len)throws IOException { out.write(b, off, len); this.transferred += len; this.listener.transferred(this.transferred); } publicvoidwrite(int b)throws IOException { out.write(b); this.transferred++; this.listener.transferred(this.transferred); } } }
API call method
privateclassregisterCallextendsAsyncTask<Void, Integer, String> { @OverrideprotectedvoidonPreExecute() { super.onPreExecute(); } @OverrideprotectedvoidonProgressUpdate(Integer... progress) { } @Overrideprotected String doInBackground(Void... params) { return registerMultipartCall(); } private String registerMultipartCall() { StringresponseString=null; HttpClienthttpclient=newDefaultHttpClient(); HttpPosthttppost=newHttpPost("URL"); //UPLOAD URLtry { AndroidMultiPartEntityentity=newAndroidMultiPartEntity(newAndroidMultiPartEntity.ProgressListener() { @Overridepublicvoidtransferred(long num) { } }); FilesourceFile=newFile(filePath); FilesourceFile2=newFile(filePath); entity.addPart("images[0]", newFileBody(sourceFile)); entity.addPart("images[1]", newFileBody(sourceFile2)); //Do your stuff multiple files entity.addPart("file_type", newStringBody("image"); entity.addPart("file_form", newStringBody("post"); httppost.addHeader("Accept","application/json"); httppost.addHeader("Content-Type", "multipart/form-data"); httppost.addHeader("Authorization", "Bearer " + sharedPreferencesManager.retreiveString(AppText.ACCESS_TOKEN)); httppost.setEntity(entity); // Making server callHttpResponseresponse= httpclient.execute(httppost); HttpEntityr_entity= response.getEntity(); intstatusCode= response.getStatusLine().getStatusCode(); if (statusCode == 200) { // Server response statusCode = 200; responseString = EntityUtils.toString(r_entity); } else { statusCode = 200; responseString = "Error occurred! Http Status Code: " + statusCode; } } catch (ClientProtocolException e) { responseString = e.toString(); } catch (IOException e) { responseString = e.toString(); } return responseString; } @OverrideprotectedvoidonPostExecute(String result) { hideProgressDialog(); }
}
5.Calling Async
new registerCall().execute();
Solution 2:
Just replace getByteData() method with this below code:
private dataPartList = ArrayList();
@OverrideprotectedMap<String, List<DataPart>> getByteDataList(){
Map<String, List<DataPart>> params = newHashMap<>();
params.put("yourKey","dataPartList");
return params
}
Post a Comment for "How To Upload Array Of Image Files Using Multipart In Android Volley Library"