通过httpPost将base64图像上传到服务器时出现问题

时间:2012-11-15 15:15:32

标签: android json heap android-image

我正在为一个应用程序进行原型设计,我使用JSON和HttpPost将本地SQLite数据库的全部内容发送到远程MySQL数据库。

一切都适用于文本数据。 现在我将图像添加到派对中,虽然我可以将图像作为base64字符串添加到我发送的JSON中。我的图像是800 x 600像素,每个大小或多或少500kb。

如果我将我的应用程序生成的JSON手动粘贴到网页上,那很好,我会得到我的图片和其他所有内容。

使用应用程序上传脚本,我尝试上传一个包含4张图片的JSON字符串,但应用程序卡在我的进度对话框中,Logcat一遍又一遍地显示:

11-15 14:32:27.809: I/dalvikvm-heap(15562): Grow heap (frag case) to 21.964MB for 2680048-byte allocation
11-15 14:32:27.840: D/dalvikvm(15562): GC_CONCURRENT freed 1744K, 30% free 20666K/29447K, paused 2ms+3ms
11-15 14:32:27.879: D/dalvikvm(15562): GC_FOR_ALLOC freed 4362K, 39% free 18049K/29447K, paused 16ms
11-15 14:32:27.918: D/dalvikvm(15562): GC_FOR_ALLOC freed 0K, 33% free 19794K/29447K, paused 22ms
11-15 14:32:27.918: I/dalvikvm-heap(15562): Grow heap (frag case) to 21.964MB for 2680276-byte    allocation
11-15 14:32:27.958: D/dalvikvm(15562): GC_CONCURRENT freed 1744K, 30% free 20667K/29447K, paused 1ms+4ms
11-15 14:32:27.997: D/dalvikvm(15562): GC_FOR_ALLOC freed 4362K, 39% free 18049K/29447K, paused 17ms
11-15 14:32:28.028: D/dalvikvm(15562): GC_FOR_ALLOC freed 0K, 33% free 19795K/29447K, paused 17ms

它会永远持续下去。我打赌这些图像太大了,无法以这种方式发送,和/或我正在某处泄漏内存。

将大尺寸图像和JSON数据上传到服务器的更好方法是什么?否则,我该如何避免内存泄漏?

代码很标准......

从Cursor到JSON:

private JSONObject get_images_data_JSON(Cursor c) {

    JSONObject image_jo = new JSONObject();

    //get unit identifier
    long unit_identifier = c.getLong(c.getColumnIndex("_id"));

    //set unit details
    try {

        image_jo.put("_id", unit_identifier);
        image_jo.put("unit_id", c.getLong(c.getColumnIndex("unit_id")));

        //encode blob in Base64 for json parsing
        String encodedImage = Base64.encodeToString(c.getBlob(c.getColumnIndex("image")), Base64.DEFAULT);

        image_jo.put("image", encodedImage);
        image_jo.put("caption", c.getString(c.getColumnIndex("caption")));


    } catch (JSONException e) {
        e.printStackTrace();
    }

    return image_jo;

}//end get_images_data_JSON

和POST功能:

public String postData(JSONArray array) {

    String responseMessage = "";

    //set connection timeout values
    HttpParams myParams = new BasicHttpParams();

    //set timeout in milliseconds until a connection is established
    HttpConnectionParams.setConnectionTimeout(myParams, 10000);

    //set timeout for waiting data
    HttpConnectionParams.setSoTimeout(myParams, 10000);
    HttpClient httpclient = new DefaultHttpClient(myParams);

    //Get a string out of the JSONArray
    String json = array.toString();

    try {

        HttpPost httppost = new HttpPost(URL);
        httppost.setHeader("Content-type", "application/json");

        StringEntity se = new StringEntity(json);
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httppost.setEntity(se);

        HttpResponse response = httpclient.execute(httppost);
        InputStream inputStream = response.getEntity().getContent();
        responseMessage = inputStreamToString(inputStream);

        //log out response from server
        longInfo(responseMessage);

    }
    //show error if connection not working
    catch (SocketTimeoutException e) {
        e.printStackTrace();
        responseMessage = "unreachable";

    }

    catch (ConnectTimeoutException e) {
        e.printStackTrace();
        responseMessage = "unreachable";

    }

    catch (Exception e) {
        e.printStackTrace();
        responseMessage = "unreachable";

    }



    return responseMessage;

}

非常感谢任何帮助

2 个答案:

答案 0 :(得分:1)

您的解决方案只会加载要发送到内存的所有数据。这几乎不可扩展。请考虑使用多部分实体。

File image1;
MultipartEntity entity = new MultipartEntity();
entity.addPart("json", new StringBody(serializedJson, Charset.forName("UTF-8"));
entity.addPart("image1", new FileBody(image1, "application/octet-stream");

此示例显示了我即将发送磁盘上可用文件的情况。通过为AbstractContentBody创建自己的实现,您应该能够发送位于数据库中的内容,而不会占用太多内存......

答案 1 :(得分:0)

对于任何来到这里的人,我解决了Vajk建议之后的所有问题,这是我的新POST功能:

public String postData(Cursor images, JSONArray json_array) {

    int counter = 0;
    String responseMessage = "";

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(URL);

        //convert JSON array to String
        String json_encoded_string = json_array.toString();

        //MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        MultipartEntity entity = new MultipartEntity();

        //add json data
        entity.addPart("json", new StringBody(json_encoded_string));

        //get all images plus data and add them to the Multipart Entity

        for (images_cursor.moveToFirst(); !images_cursor.isAfterLast(); images_cursor.moveToNext()) {

            counter++;

            //Get image as byte array
            byte[] image_ba = images_cursor.getBlob(images_cursor.getColumnIndex("image"));
            long image_unit_id = images_cursor.getLong(images_cursor.getColumnIndex("unit_id"));
            String image_caption = images_cursor.getString(images_cursor.getColumnIndex("caption"));

            //add image to multipart
            entity.addPart("image" + counter, new ByteArrayBody(image_ba, "image" + counter + ".jpg"));

            //add unit _id to multipart
            entity.addPart("image_unit_id" + counter, new StringBody(String.valueOf(image_unit_id)));

            //add caption to multipart
            entity.addPart("image_caption" + counter, new StringBody(String.valueOf(image_caption)));

        }

        httppost.setEntity(entity);

        HttpResponse response = httpclient.execute(httppost);
        InputStream inputStream = response.getEntity().getContent();
        responseMessage = inputStreamToString(inputStream);

        //log out response from server
        longInfo(responseMessage);

    }
    //show error if connection not working
    catch (SocketTimeoutException e) {
        e.printStackTrace();
        responseMessage = "unreachable";

    }

    catch (ConnectTimeoutException e) {
        e.printStackTrace();
        responseMessage = "unreachable";

    }

    catch (Exception e) {
        e.printStackTrace();
        responseMessage = "unreachable";

    }

    return responseMessage;
}

有人可能觉得这很有用;)