Search code examples
imagegoogle-app-enginefile-uploadblobstore

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.


Solution

  • 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;
    
    }