我有一个存储为字节数组的图像(也可以作为 .jpg 从 Android 目录访问),我正在尝试获取图像的宽度和高度。我尝试使用 javax.imageio 但是 Dalvik 不支持这个库,所以我选择使用 Bitmap。有人可以帮我从这里出去吗?我不断收到空指针异常。
public int getWidth(byte[] image) throws IOException{
Bitmap bitmapImage = BitmapFactory.decodeFile(image.toString());
int imageWidth = bitmapImage.getWidth();
return imageWidth;
}
干杯。
最佳答案
您可以尝试使用 decodeByteArray(image)
而不是 decodeFile(new String(image))
。留下信息供将来引用:
toString()
不适用于数组(它返回一些随机字符串)。由于您向 decodeImage
传递了无效字符串,因此它会返回 null
以指示未找到该字符串。因此,如果您调用其上的方法,则会导致 NullPointerException
。如果字节数组使用字符集进行编码,请使用可以正确解码字节数组的 new String(image)
,而不是使用 image.toString()
。
关于java - Android:从字节数组或文件获取高度/宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16929562/