我正在将Google App Engine端点与我的Android应用结合使用。在我的一个端点中,我有一个方法来获取以Base64编码的图像,然后将其存储在Blobstore中。检索图像是通过Google ImageService的服务URL完成的。
因此,我遇到了两个问题。首先,不赞成使用我使用的Blobstore File API。其次,调用速度很慢,因为服务器在存储Blob时会同步工作,之后再在serving-url和blob-key上工作。
所以我的问题是,我该如何更改代码以使用Google(servlets)提出的Blobstore,但又要继续在Android代码中使用非常漂亮的Endpoint。有没有办法在不使用HttpRequest类的情况下继续使用该方法?
简而言之:
这是我的代码。
从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);
}
这不是您所解释问题的解决方案,但是您可以使用此GCS:
Google Cloud Storage Java api有很多可用的,您可以上传图像或任何文件,并且可以将其公开并获取图像url,或者只是从GCS获取JSON api,以便可以下载图像或任何文件内容。
例如:https://github.com/pliablematter/simple-cloud-storage
您可以使用Google Cloud Storage Java API将数据发送到云端,
在Android中使用Google Cloud Storage JSON API检索数据。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句