Last active
February 2, 2017 05:49
-
-
Save mbarcia/8660924 to your computer and use it in GitHub Desktop.
Uploading a jpeg image with Volley. Part of http://develop-for-android.blogspot.com.es/2014/01/using-volley-in-your-application.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.colaborativa.exampleapp.api; | |
import java.io.ByteArrayOutputStream; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.charset.Charset; | |
import java.util.Collections; | |
import java.util.HashMap; | |
import java.util.Map; | |
import org.apache.http.entity.ContentType; | |
import org.apache.http.entity.mime.HttpMultipartMode; | |
import org.apache.http.entity.mime.MultipartEntityBuilder; | |
import com.android.volley.AuthFailureError; | |
import com.android.volley.NetworkResponse; | |
import com.android.volley.Request; | |
import com.android.volley.Response; | |
import com.android.volley.Response.ErrorListener; | |
import com.android.volley.Response.Listener; | |
import com.android.volley.VolleyLog; | |
import com.android.volley.toolbox.HttpHeaderParser; | |
public class PhotoMultipartRequest<T> extends Request<T> { | |
private static final String FILE_PART_NAME = "file"; | |
private MultipartEntityBuilder mBuilder = MultipartEntityBuilder.create(); | |
private final Response.Listener<T> mListener; | |
private final File mImageFile; | |
protected Map<String, String> headers; | |
public PhotoMultipartRequest(String url, | |
ErrorListener errorListener, | |
Listener<T> listener, | |
File imageFile) | |
{ | |
super(Method.POST, url, errorListener); | |
mListener = listener; | |
mImageFile = imageFile; | |
buildMultipartEntity(); | |
} | |
@Override | |
public Map<String, String> getHeaders() throws AuthFailureError { | |
Map<String, String> headers = super.getHeaders(); | |
if (headers == null | |
|| headers.equals(Collections.emptyMap())) { | |
headers = new HashMap<String, String>(); | |
} | |
headers.put("Accept", "application/json"); | |
return headers; | |
} | |
private void buildMultipartEntity() | |
{ | |
mBuilder.addBinaryBody(FILE_PART_NAME, mImageFile, ContentType.create("image/jpeg"), mImageFile.getName()); | |
mBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); | |
mBuilder.setLaxMode().setBoundary("xx").setCharset(Charset.forName("UTF-8")); | |
} | |
@Override | |
public String getBodyContentType() | |
{ | |
String contentTypeHeader = mBuilder.build().getContentType().getValue(); | |
return contentTypeHeader; | |
} | |
@Override | |
public byte[] getBody() throws AuthFailureError | |
{ | |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
try | |
{ | |
mBuilder.build().writeTo(bos); | |
} | |
catch (IOException e) | |
{ | |
VolleyLog.e("IOException writing to ByteArrayOutputStream bos, building the multipart request."); | |
} | |
return bos.toByteArray(); | |
} | |
@Override | |
protected Response<T> parseNetworkResponse(NetworkResponse response) | |
{ | |
T result = null; | |
return Response.success(result, HttpHeaderParser.parseCacheHeaders(response)); | |
} | |
@Override | |
protected void deliverResponse(T response) | |
{ | |
mListener.onResponse(response); | |
} | |
} |
I have tried above code as it is but It will crash when I use compile 'org.apache.httpcomponents:httpmime:4.5.2' and getting null pointer exception with compile 'org.apache.httpcomponents:httpmime:4.3.6'.
RequestFuture future = RequestFuture.newFuture();
File file = new File(userQuery.getFilePath());
PhotoMultipartRequest imageUploadReq = new PhotoMultipartRequest(url, future, errorListener, file);
Directory.getInstance().addToRequestQueue(imageUploadReq,
SV.TAG_UPLOAD_PROFILE_IMAGE);
try {
return future.get(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
errorListener.onErrorResponse(new VolleyError(e));
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I need send my image file to API in PHP. The parameter in form-data is "picture". How I do it with you example?
Which parameters in Header I need put?
You have an example utilizing this class?
Help me.