错误:java.lang.UnsupportedOperationException:使用App Engine的BlobStore和Image API时没有可用的图像数据

Mas*_*ind 6 google-app-engine file-upload image blobstore

我需要使用App Engine BlobStore检索上传图像的高度和宽度.为了找到我使用以下代码:

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

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

        }
Run Code Online (Sandbox Code Playgroud)

我可以上传图像并生成BlobKey但是当将Blobkey传递给makeImageFromBlob()时,它会生成以下错误:

java.lang.UnsupportedOperationException:没有可用的图像数据

如何解决这个问题或任何其他方式直接从BlobKey找到图像的高度和宽度.

Mas*_*ind 7

Image上的大多数方法本身都会抛出UnsupportedOperationException.所以我使用com.google.appengine.api.blobstore.BlobstoreInputStream.BlobstoreInputStream来操作blobKey中的数据.这是我可以获得图像宽度和高度的方式.

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;

}
Run Code Online (Sandbox Code Playgroud)