Android アプリから Google App Engine データストアに画像を送信する必要があります。Blob
この画像は、内にデータ型として埋め込む必要がありますJSONObject
。
デバイスのカメラから画像をキャプチャし、jpg 形式に圧縮できます。次にメソッドのByteArrayOutputStream
fromを使用Bitmap.compress()
してバイト配列を作成します。
問題は、このバイト配列をに配置 ( put()
/ ) する方法です。私は次のことを試しました。つまり、バイト配列をaccumulate()
JSONObject
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;
}
これはサーバー側で次のエラーで失敗します:
Expected BEGIN_OBJECT but was BEGIN_ARRAY。私がやっていることが間違っていることはわかっていますが、バイト配列をに埋め込む正しい方法は何JSONObject
ですか?
編集:私は Blobstore について知っています。それが私の最後の手段です。これを機能させる試みは、Blobstore を回避する試みではありません。