Error :java.lang.UnsupportedOperationException: No image data is available when using App Engine's BlobStore and Image API
Asked Answered
N

3

6

I need to retrieve the height and width of uploaded image using App Engine BlobStore. For finding that i used following code :

try {
            Image im = ImagesServiceFactory.makeImageFromBlob(blobKey);

            if (im.getHeight() == ht && im.getWidth() == wd) {
                flag = true;
            }
        } catch (UnsupportedOperationException e) {

        }

i can upload the image and generate the BlobKey but when pass the Blobkey to makeImageFromBlob(), it generate the following error:

java.lang.UnsupportedOperationException: No image data is available

How to solve this problem or any other way to find image height and width directly from BlobKey.

Nipper answered 25/7, 2012 at 13:13 Comment(0)
N
7

Most of the methods on the Image itself will currently throw UnsupportedOperationException. So i used com.google.appengine.api.blobstore.BlobstoreInputStream.BlobstoreInputStream to manipulate data from blobKey. That's way i can get image width and height.

byte[] data = getData(blobKey);
Image im = ImagesServiceFactory.makeImage(data);
if (im.getHeight() == ht && im.getWidth() == wd) {}
private byte[] getData(BlobKey blobKey) {
    InputStream input;
    byte[] oldImageData = null;
    try {
        input = new BlobstoreInputStream(blobKey);
                ByteArrayOutputStream bais = new ByteArrayOutputStream();
        byte[] byteChunk = new byte[4096];
        int n;
        while ((n = input.read(byteChunk)) > 0) {
            bais.write(byteChunk, 0, n);
        }
        oldImageData = bais.toByteArray();
    } catch (IOException e) {}

    return oldImageData;

}
Nipper answered 26/7, 2012 at 5:49 Comment(0)
G
4

If you can use Guava, the implementation is easier to follow:

public static byte[] getData(BlobKey blobKey) {
    BlobstoreInputStream input = null;
    try {
        input = new BlobstoreInputStream(blobKey);
        return ByteStreams.toByteArray(input);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        Closeables.closeQuietly(input);
    }
}

The rest remains the same.

Gettings answered 18/9, 2012 at 8:47 Comment(0)
S
0

Another possibility is to make a useless transformation on the image (as rotating by 0 degree)

Image oldImage = ImagesServiceFactory.makeImageFromFilename(### Filepath ###);
Transform transform = ImagesServiceFactory.makeRotate(0);
oldImage = imagesService.applyTransform(transform,oldImage);

After that transformation, you may get width & height of the image as expected:

oldImage.getWidth();

Even if this works, this transformation influences the performance negatively ;)

Spectra answered 14/10, 2016 at 8:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.