0

ユーザーのプロフィール写真を保持するエンティティがあります。簡単にするために、サービングURLを保存するだけです。ユーザーが画像を切り抜くことができるようにしています。これを行うには、サービングURLから画像を取得し、作成します。com.google.appengine.api.images.Image;この画像はバイト配列から作成されたため、blobキーがありません。この画像をBLOBとして保存し、提供URLを取得する最も簡単な方法は何ですか?

URL url = new URL(currentProfile.getProfilePictureUrl());
InputStream input = url.openStream();
byte[] byteArray = IOUtils.toByteArray(input);
Image newImage = ImagesServiceFactory.makeImage(byteArray);

int imageWidth = newImage.getWidth();
int imageHeight = newImage.getHeight();

ImagesService imagesService = ImagesServiceFactory.getImagesService();
float lx = (float)x/(float)imageWidth;
float ty =  (float)y/(float)imageHeight;
float rx = (float)x2/(float)imageWidth;
float by = (float)y2/(float)imageHeight;
Transform resize = ImagesServiceFactory.makeCrop(lx, ty, rx, by);

Image transImage = imagesService.applyTransform(resize, newImage);

BlobstoreService blobStoreService = BlobstoreServiceFactory.getBlobstoreService();

//This doesn't work because transImage.getBlobKey() is null
ServingUrlOptions options =  ServingUrlOptions.Builder.withBlobKey(transImage.getBlobKey());
4

1 に答える 1

4

プログラムで blobstore にデータを書き込むことができます。私はこの小さなスニペットを使用します:

private BlobKey saveToBlobstore(String contentType, String imageName, byte[] imageData) throws IOException {
    // Get a file service
    FileService fileService = FileServiceFactory.getFileService();

    // Create a new Blob file and set the name to contain ref to UserImage
    AppEngineFile file = fileService.createNewBlobFile(contentType, imageName);

    // Open a channel to write to it
    FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);

    writeChannel.write(ByteBuffer.wrap(imageData));
    writeChannel.closeFinally();

    // return the BlobKey
    return fileService.getBlobKey(file);
}
于 2013-01-25T07:04:49.720 に答える