-1

I need to send an image from my Android app to Google App Engine datastore. This image needs to be embedded as a Blob datatype inside a JSONObject.

I am able to capture the image from the device camera and compress it to the jpg format. I then use the ByteArrayOutputStream from the Bitmap.compress() method to create a byte array.

The question is, how do I place (put()/accumulate()) this byte array into the JSONObject. I have tried the following, i.e., converting the byte array to a JSONArray

private JSONObject  createJSONObject() {

    byte[]                  bryPhoto       =    null;
    ByteArrayInputStream    bais           =    null;
    ByteArrayOutputStream   baos           =    null;
    JSONArray               jryPhoto       =    null;
    JSONObject              jbjToBeSent    =    null;

    baos        =   new ByteArrayOutputStream();
    jbjToBeSent =   new JSONObject();

    try {
        jbjToBeSent.accumulate("hwBatch", strBatch);
        jbjToBeSent.accumulate("hwDescription", etDescription.getText().toString());

        if(null == bmpPhoto) {
             bryPhoto   =   null;
        }
        else {
            bmpPhoto.compress(Bitmap.CompressFormat.JPEG, 10, baos);
            bryPhoto    =    baos.toByteArray();
            bais        =    new ByteArrayInputStream(bryPhoto);
        }
        jryPhoto        =    readBytes(bais);
        jbjToBeSent.accumulate("hwPhoto", jryPhoto);
    }

    catch(JSONException je) {
        // Omitted exception handling code to improve readability
    }
    return jbjToBeSent;
}

public JSONArray readBytes(InputStream inputStream) {
    JSONArray    array    =    null;
    try {
        int       bufferSize    =    1024;
        byte[]    buffer        =    new byte[bufferSize]; 
        array                   =    new JSONArray();
        int       len           =    0;
        int       i             =    0;
        while ((len = inputStream.read(buffer)) != -1) {  
            ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); 
            byteBuffer.write(buffer, 0, len); 
            byte[] b= byteBuffer.toByteArray();
            array.put(i,Base64.encodeToString(b, Base64.DEFAULT));
            i++;
        }
    }
    catch(IOException ioe) {
        // Omitted exception handling code to improve readability   
    }
    catch(JSONException jsone) {
        // Omitted exception handling code to improve readability               
    }
    return array; 
}

This fails on the server side with the error: Expected BEGIN_OBJECT but was BEGIN_ARRAY. I know what I am doing is wrong but then, what is the correct way of embedding a byte array in a JSONObject?

EDIT: I know about Blobstore and that would be my last resort. My attempt to make this work is not an attempt to circumvent the Blobstore.

EdmDroid
  • 1,350
  • 1
  • 11
  • 25

1 Answers1

-1

try this,

public JSONArray readBytes(InputStream inputStream) {
    JSONArray array =   null;
    try {
        int bufferSize  =   1024;
        byte[]  buffer      =   new byte[bufferSize]; 
        array           =   new JSONArray();
        int     len         =   0;
        int i       =   0;

        array = new JSONArray();
        while ((len = inputStream.read(buffer)) != -1) {  
            ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); 
            byteBuffer.write(buffer, 0, len); 
            byte[] b= byteBuffer.toByteArray();
            array.put(i,Base64.encodeToString(b, Base64.DEFAULT));
            i++;
        }
    }
    catch(IOException ioe) {

    }
    catch(JSONException jsone) {

    }

    return array; 
}
Akash Moradiya
  • 3,318
  • 1
  • 14
  • 19