-1

Android アプリから Google App Engine データストアに画像を送信する必要があります。Blobこの画像は、内にデータ型として埋め込む必要がありますJSONObject

デバイスのカメラから画像をキャプチャし、jpg 形式に圧縮できます。次にメソッドのByteArrayOutputStreamfromを使用Bitmap.compress()してバイト配列を作成します。

問題は、このバイト配列をに配置 ( put()/ ) する方法です。私は次のことを試しました。つまり、バイト配列をaccumulate()JSONObjectJSONArray

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

これはサーバー側で次のエラーで失敗します: Expected BEGIN_OBJECT but was BEGIN_ARRAY。私がやっていることが間違っていることはわかっていますが、バイト配列をに埋め込む正しい方法は何JSONObjectですか?

編集:私は Blobstore について知っています。それが私の最後の手段です。これを機能させる試みは、Blobstore を回避する試みではありません。

4

1 に答える 1

-1

これを試して、

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; 
}
于 2014-05-24T10:52:10.713 に答える