【发布时间】:2014-11-14 02:33:59
【问题描述】:
我正在使用与我的 Android 应用相关的 Google App Engine Endpoints。在我的一个端点中,我有一种方法可以获取图像 - 以 Base64 编码 - 然后将其存储在 Blobstore 中。检索图像是通过 Google ImageService 的服务 URL 完成的。
所以,我遇到了两个问题。首先,我使用的 Blobstore 文件 API 已弃用。其次,调用非常慢,因为服务器在存储 blob 时同步工作,稍后在服务 URL 和 blob-key 上工作。
所以我的问题是,如何更改代码以使用 Google(servlets)建议的 Blobstore,但在 Android 代码中继续使用我非常好的端点。有没有办法在不使用 HttpRequest 类的情况下继续使用该方法?
简而言之:
- 我可以保留对端点的客户端调用还是需要更改该代码?
- 如果我可以保留端点的客户端/服务器端接口,我如何重定向到 Blobstore 以异步保存图像,然后调用另一个可以存储 blobkey 和服务 URL 的 servlet?
这是我的代码。
从 Android 客户端发送到 Google 应用引擎的实体。
@Entity
public class Image {
@Id
private int id = -1;
private byte[] data;
private String mimeType;
private int width;
private int height;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
public String getMimeType() {
return mimeType;
}
public void setMimeType(String mimeType) {
this.mimeType = mimeType;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
服务器端端点实现:
@Api(name = "imageEndpoint", namespace = @ApiNamespace(ownerDomain = "example.com", ownerName = "example.com", packagePath = "myPackage")}
public class ImageEndPoint extentds BaseEndPoint {
@ApiMethod(name = "updateImage")
public Void updateImage(final User user,
final Image image) {
String blobKey = FileHandler.storeFile(
location != null ? location.getBlobKey() : null,
image.getData(), image.getMimeType(),
LocationTable.TABLE_NAME + "." + locationId, logger);
ImagesService imageService = ImagesServiceFactory
.getImagesService();
// image size 0 will retrieve the original image (max: 1600)
ServingUrlOptions options = ServingUrlOptions.Builder
.withBlobKey(new BlobKey(blobKey)).secureUrl(true)
.imageSize(0);
String servingUrl = imageService.getServingUrl(options);
// store BlobKey & ServingUrl in Database
return null;
}
}
在 Google App Engine 上调用端点的 Android Java 代码。
public void uploadBitmap(Bitmap image)
{
ImageEndpoint.Builder endpointbuilder = creator.create(
AndroidHttp.newCompatibleTransport(), new JacksonFactory(),
new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest arg0)
throws IOException {
}
});
endpointbuilder.setApplicationName(GoogleConstants.PROJECT_ID);
ImageEndpoint endpoint = endpointbuilder.build();
// set google credidentials
// encode image to byte-rray
byte[] data = ....
// create cloud contet
Image content = new Image();
content.setWidth(image.get_width());
content.setHeight(image.get_height());
content.setMimeType("image/png");
content.setData(Base64.encodeBase64String(data));
// upload content (but takes too much time)
endpoint.updateImage(content);
}
【问题讨论】:
-
将其移植到 Google Cloud Storage 的示例是否可以接受?您是否只使用 Files API 是因为您希望通过 Endpoints 上传图像(例如使用 Android 相机拍摄的照片),还是有其他原因?此外,您是使用图像服务在应用程序的所有其他位置转换图像,还是仅使用它来提供原始原始图像? (您发布的代码仅提供未转换的图像,这可以在没有图像服务的情况下完成。)
-
我正在使用两个图片服务网址。一个是原始图像,另一个是将图像缩小到 320x240。我想坚持使用端点,但我也可以更改为 servlet。但是我需要一个使用端点方法中使用的 Google 用户的示例。这是一个注入参数,如果它不是端点,我不知道在 Android 上添加它。这是因为不是每个用户都可以在我的应用中上传图片。
标签: java android google-app-engine google-cloud-endpoints blobstore