Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 861
Master Mind Avatar asked Jul 25 '12 13:07

Master Mind


2 Answers

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.

like image 33
Nacho Coloma Avatar answered Oct 05 '22 18:10

Nacho Coloma


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;

}
like image 53
Master Mind Avatar answered Oct 05 '22 20:10

Master Mind