Android上传更多图片导致崩溃

时间:2015-10-22 07:38:32

标签: android bitmap upload

我使用了Httpmime-4.2.5.jar。当我上传小尺寸图像时,它是正确的,但是大图像导致我的应用程序崩溃。那么我可以改进我的代码吗?有其他方法来压缩我的bx_images文件吗?

public static void upload(String url,Map<String,String> params,List<File> bx_images,final VolleyHandler<String> handler){
    params.put("token",API_AUTH_CODE);
    MultipartRequest multipartRequest = new MultipartRequest(getAbsoluteURL(url), new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            String error = volleyError.getMessage();
            handler.reqFailure(error);
        }
    }, new Response.Listener<String>() {
        @Override
        public void onResponse(String s) {
            handler.reqSuccess(s);
        }
    },bx_images.size()==1?"bx_images":"bx_images[]",bx_images,params);
    httpService.addToRequestQueue(multipartRequest);
}


public class MultipartRequest extends Request<String> {

private static final String TAG = MultipartRequest.class.getSimpleName();
private MultipartEntity entity = new MultipartEntity();

private final Response.Listener<String> mListener;

private List<File> mFileParts;
private String mFilePartName;
private Map<String, String> mParams;
/**
 * @param url
 * @param errorListener
 * @param listener
 * @param filePartName
 * @param file
 * @param params
 */
public MultipartRequest(String url, Response.ErrorListener errorListener,
                        Response.Listener<String> listener, String filePartName, File file,
                        Map<String, String> params) {
    super(Method.POST, url, errorListener);

    mFileParts = new ArrayList<File>();
    if (file != null) {
        mFileParts.add(file);
    }
    mFilePartName = filePartName;
    mListener = listener;
    mParams = params;
    buildMultipartEntity();
}
/**
 * @param url
 * @param errorListener
 * @param listener
 * @param filePartName
 * @param files
 * @param params
 */
public MultipartRequest(String url, Response.ErrorListener errorListener,
                        Response.Listener<String> listener, String filePartName,
                        List<File> files, Map<String, String> params) {
    super(Method.POST, url, errorListener);
    mFilePartName = filePartName;
    mListener = listener;
    mFileParts = files;
    mParams = params;
    buildMultipartEntity();
}

private void buildMultipartEntity() {
    if (mFileParts != null && mFileParts.size() > 0) {
        for (File file : mFileParts) {
            entity.addPart(mFilePartName, new FileBody(file));
        }
        long l = entity.getContentLength();
        Log.d(TAG,mFileParts.size() + "个,length:" + l);
    }

    try {
        if (mParams != null && mParams.size() > 0) {
            for (Map.Entry<String, String> entry : mParams.entrySet()) {
                entity.addPart(
                        entry.getKey(),
                        new StringBody(entry.getValue(), Charset
                                .forName("UTF-8")));
            }
        }
    } catch (UnsupportedEncodingException e) {
        VolleyLog.e("UnsupportedEncodingException");
    }
}

@Override
public String getBodyContentType() {
    return entity.getContentType().getValue();
}

@Override
public byte[] getBody() throws AuthFailureError {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        entity.writeTo(bos);
    } catch (IOException e) {
        VolleyLog.e("IOException writing to ByteArrayOutputStream");
    }
    return bos.toByteArray();
}

@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
    Log.d(TAG,"parseNetworkResponse");
    if (VolleyLog.DEBUG) {
        if (response.headers != null) {
            for (Map.Entry<String, String> entry : response.headers
                    .entrySet()) {
                VolleyLog.d(entry.getKey() + "=" + entry.getValue());
            }
        }
    }

    String parsed;
    try {
        parsed = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers));
    } catch (UnsupportedEncodingException e) {
        parsed = new String(response.data);
    }
    return Response.success(parsed,
            HttpHeaderParser.parseCacheHeaders(response));
}


/*
 * (non-Javadoc)
 *
 * @see com.android.volley.Request#getHeaders()
 */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    VolleyLog.d("getHeaders");
    Map<String, String> headers = super.getHeaders();

    if (headers == null || headers.equals(Collections.emptyMap())) {
        headers = new HashMap<String, String>();
    }


    return headers;
}

@Override
protected void deliverResponse(String response) {
    mListener.onResponse(response);
}

}

0 个答案:

没有答案