2

After using the ImagesService to transform an uploaded image, I would like to store it back into a new Blob file and make it available through getServingUrl() as provided by the ImagesService.

Storing the image in a new AppEngineFile as described here works fine and I am able to open and view it locally using the dev server.

However when passing the blobKey for the new AppEngineFile to ImagesService.getServingUrl() a

java.lang.IllegalArgumentException: Could not read blob.

exception is thrown. Any ideas what the problem could be? This is the code I use to transform and store an uploaded image (blobKey and blobInfo correspond to the uploaded file, not the newly created one).

/* Transform image in existing Blob file */
Image originalImage = ImagesServiceFactory.makeImageFromBlob(blobKey);
Transform verticalFlip = ImagesServiceFactory.makeVerticalFlip();
ImagesService imagesService = ImagesServiceFactory.getImagesService();
Image newImage = imagesService.applyTransform(verticalFlip, originalImage);

/* Store newImage in an AppEngineFile */
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(blobInfo.getContentType());
FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);
ByteBuffer buffer = ByteBuffer.wrap(newImage.getImageData());
writeChannel.write(buffer);

/* closeFinally assigns BlobKey to new file object */
writeChannel.closeFinally();
BlobKey newBlobKey = fileService.getBlobKey(file);

Edit:

The above code is correct, the problem was storing a String representation of the new blob key using newBlobKey.toString() instead of newBlobKey.getKeyString().

Arno Fiva
  • 1,459
  • 1
  • 13
  • 17

2 Answers2

0

Why would you want to do that? Once you transform an image it is cached and anyway it is always fast. If you really feel you want to save it just use urlfetch to read the data and store them in the BlobStore ;-)

PanosJee
  • 3,866
  • 6
  • 36
  • 49
  • An imageServingUrl is able to crop and resize pics, but the imageService can do more like rotating. Therefore the idea was to have an image uploaded into Blob store, rotate it, store it back to the Blob store and then create an imageServingUrl of the rotated image to provide previews at different scales. The code above actually works, I have to update the question/add an answer. – Arno Fiva Jul 08 '11 at 17:17
0

The following works fine when executed at the end of the code posted in the question:

String url = imagesService.getServingUrl(newBlobKey)

The URL can then be used to scale and crop the new image as described in the docs http://code.google.com/appengine/docs/java/images/overview.html#Transforming_Images_from_the_Blobstore

Arno Fiva
  • 1,459
  • 1
  • 13
  • 17