我需要使用 App Engine BlobStore 检索上传图像的高度和宽度。为了发现我使用了以下代码:
try {
Image im = ImagesServiceFactory.makeImageFromBlob(blobKey);
if (im.getHeight() == ht && im.getWidth() == wd) {
flag = true;
}
} catch (UnsupportedOperationException e) {
}
我可以上传图像并生成 BlobKey,但是当将 Blobkey 传递给 makeImageFromBlob() 时,它会生成以下错误:
java.lang.UnsupportedOperationException: No image data is available
如何解决这个问题或任何其他直接从 BlobKey 查找图像高度和宽度的方法。
最佳答案
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;
}
关于image - 错误 :java. lang.UnsupportedOperationException:使用 App Engine 的 BlobStore 和图像 API 时没有图像数据可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11650518/