私は現在、App Engine バックエンドとの間で画像をやり取りする必要があるアプリに取り組んでいます。もともと私はエンドポイントを介して直接画像を送信することを計画していました (それらは小さい - 最大 100kb - 平均 20kb) ですが、エンドポイントを介してデータをバイト配列として送信すると、(残りの API から) データが無効な文字があります。これを回避する方法はありますか?
私の 2 番目の試みは、BlobService を使用し、以下のコードを使用してアップロード URL をクライアントに返すことでした。
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
UploadOptions uploadOptions = UploadOptions.Builder.withGoogleStorageBucketName("bucketname").maxUploadSizeBytes(1048576);
String url = blobstoreService.createUploadUrl("/uploaded", uploadOptions);
次に、Android デバイスで HTTP ポストを使用して画像をアップロードします。
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("data", new ByteArrayBody(data,"image/png","img.png"));
httppost.setEntity(entity);
String res = EntityUtils.toString( httpclient.execute(httppost).getEntity(), "UTF-8");
これはうまくいくようで、画像がアップロードされます。ただし、このアップロードされた画像のブロブキーを取得する方法がわかりません。誰か知っていますか?また、HTTP 投稿の結果は 404 エラーです。"/uploaded" ページが存在しないためでしょうか?
第 3 に、blobkey を手動で入力し、それを使用して次のコードで URL を返し、画像を提供する場合:
private String getImage(){
return getThumbUrl(new BlobKey("encoded_gs_key:ZGNpbWcxMy93czZwZ2lUeXdpY0xvZ2xtZGpHZ2dn"));
}
private String getThumbUrl(BlobKey blobkey){
ServingUrlOptions options = ServingUrlOptions.Builder.withBlobKey(blobkey);
try {
return ImagesServiceFactory.getImagesService().getServingUrl(options);
} catch(IllegalArgumentException e) {
e.printStackTrace();
return null;
} catch(ImagesServiceFailureException e) {
e.printStackTrace();
return null;
}
}
画像の URL を受け取りましたが、画像の色がすべてめちゃくちゃです。インデックス付きの png をアップロードしています... ImageService がそれらを正しく処理できるかどうかわかりません。それができない場合、ImageService ではなく BlobstoreService.serve() を介して画像を直接提供するにはどうすればよいですか?
これは、ImageService URL からの結果の画像の画像です: http://i.imgur.com/EhfkJ9j.png
乾杯、ベン