开发者

How to post large image using base64 in android?

Currently I am working one project which requires to post the data on the server database. Here I post the data like name, id, image, etc.

I want to pass the image as a binary data. For that I am converting the image into a base64 string and then POST-ing the data, but due to the large image resolution or size HTTP connection, I get the 413 error code which tells that the URL is to large.

Please can anyone give me the solution for this? Here I give you my code which I had implemented.

values

Bitmap b1 = (Bitmap) data.getExtras().get("data");
b1=Bitmap.createScaledBitmap(b1, 100,100, true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
b1.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
byte[] b = baos.toByteArray(); 
String base64String = Base64.encodeBytes(b);

Log.e("Base 64 conversion of image--------", base64String);

response = webService.doNetworkConnection("{\"method\":\"EventUploadPhoto\",\"data\":{\"ClientCode\":"
    + "\""
    + Util.clientCode
    + "\""
    + ",\"EventID\":"
    + "\""
    + itemId
    + "\""
    + ",\"ImageName\":"
    + "\""
    + filename
    + System.currentTimeMillis()
    + "\""
    + ",\"ImageData\":"
    + "\""
    + imgData
    + "\""
    + ",\"UserName\":"
    + "\""
    + username
    + "\""
    + ",\"TokenId\":"
    + "\""
    + tokenid
    + "\"" + "}}", "POST");

doNetworkConnection

public String doNetworkConnection(String request, String methodType) throws IOException {
    String str = java.net.URLEncoder.encode(request, "UTF-8");
    String resultstring = "";
    System.out.println("string request:" + request);

    int response = -1;

    try {
        URL url = new URL((mContext.getResources()
                        .getString(R.string.WebServiceUrl))
                        + "" + str);
        System.out.println("url:" + url);
        URLConnection conn = url.openConnection();

        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        httpConn.connect();

        response = httpConn.getResponseCode();
        System.out.println("response code:" + response);

        if (response == HttpURLConnection.HTTP_OK) {
            InputStream is = httpConn.getInputStream();
            resultstring = convertinputStreamToString(is);
        } else {
            resultstring = "";             
开发者_C百科        }
    } catch (Exception e) {
        resultstring = "";         
    }   
    System.out.println("result string:" + resultstring);

    return resultstring;
}

Here the HTTP response code is 413, so it tells that the URL is to large and can not POST the image.


If you're set on HTTP: Since it's just a String, you can break it up into separate Strings and POST them individually. Then concat them together on the other side.

Otherwise, you can use FTP or get lower and stream it over TCP/IP. There are Java libraries like this one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜