[英]from deprecated Blobstore File API to serving blobs
我正在使用與我的Android應用程序相關的Google App Engine端點。 在我的一個端點中,我有一個方法,它采用Base64編碼的圖像,然后存儲在Blobstore中。 檢索圖像是通過Google ImageService的服務URL完成的。
所以,我有兩個問題。 首先,我不推薦使用我正在使用的Blobstore File API。 其次,調用非常慢,因為服務器在存儲blob時同步工作,稍后在serve-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雲端存儲java api可用,您可以上傳圖像或任何文件,您可以將其公開並獲取圖像URL或只是從GCS獲取JSON api,以便您可以下載圖像或任何文件內容。
例如: https : //github.com/pliablematter/simple-cloud-storage
您可以使用Google Cloud Storage Java API將數據發送到雲端
在android中使用Google Cloud Storage JSON api來檢索數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.