我需要将我正在开发的应用程序中的图像上传到服务器,我想知道如何使用Google Volley开发Multipart Request来加载图像.
谢谢
解决方法:
我有一个通过Google Volley上传图片的示例.看一看:
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();}@OverrIDepublic 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"));}@OverrIDepublic String getbodyContentType(){ String ContentTypeheader = mBuilder.build().getContentType().getValue(); return ContentTypeheader;}@OverrIDepublic 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();}@OverrIDeprotected Response<T> parseNetworkResponse(NetworkResponse response) { T result = null; return Response.success(result, httpheaderParser.parseCacheheaders(response));}@OverrIDeprotected voID deliverResponse(T response) { mListener.onResponse(response);}}
你可以像这样使用它:
RequestQueue mQueue = Volley.newRequestQueue(context);PhotoMultipartRequest imageUploadReq = new PhotoMultipartRequest(url, ErrorListener, Listener, imagefile);mQueue.add(imageUploadReq);
我希望这些代码会激励你.
总结以上是内存溢出为你收集整理的android – 使用Google Volley上传图片全部内容,希望文章能够帮你解决android – 使用Google Volley上传图片所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)