Paranoid Android
Paranoid Android

Reputation: 5037

Problems uploading base64 images to server by httpPost

I am prototyping an application where I send the whole content of the local SQLite database to a remote MySQL database using JSON and HttpPost.

Everything works fine for text data. Now I added images to the party and I though I could add the images as base64 strings to the JSON I am sending away. My images are 800 x 600 pixels, more or less 500kb in size each.

If I paste the JSON generated by my app manually to a web page, it is fine, I get my images and everything else.

Using the app uploading script, I tried to upload a JSON string with 4 images but the application gets stuck at my progress dialog and Logcat goes on over and over showing this:

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

and it goes on forever. I bet the images are too big to be sent this way, and/or I am leaking memory somewhere.

What would be a better approach to upload big size images along with JSON data to a server? Otherwise, how can I avoid memory leaks?

The code is pretty standard...

From Cursor to 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

and the POST function:

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;

}

Any help will be greatly appreciated

Upvotes: 1

Views: 2997

Answers (2)

Paranoid Android
Paranoid Android

Reputation: 5037

For anyone coming here, I solved all the problems following Vajk advice and this is my new POST function:

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;
}

Someone might find this useful ;)

Upvotes: 1

Vajk Hermecz
Vajk Hermecz

Reputation: 5702

Your solution just loads all the data to be sent into memory. That's hardly scalable. Consider using a multipart entity instead.

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");

This example shows the case when i'm about to send a file available on the disk. By creating your own imlementation for AbstractContentBody, you should be able to send something located in your db, without consuming too much memory...

Upvotes: 1

Related Questions