Android:从字节数组或文件中获取高度/宽度

时间:2013-06-05 00:18:05

标签: java android bitmap height width

我有一个存储为字节数组的图像(也可以从Android目录中以.jpg格式访问),我正在尝试获取图像的宽度和高度。我试过使用javax.imageio但是Dalvik不支持这个lib,所以我选择使用Bitmap。有人可以帮我从这里出去吗?我一直在拿Null Pointer Exceptions。

public int getWidth(byte[] image) throws IOException{
    Bitmap bitmapImage = BitmapFactory.decodeFile(image.toString());
    int imageWidth = bitmapImage.getWidth();

    return imageWidth;
}

干杯。

1 个答案:

答案 0 :(得分:4)

您可以尝试使用decodeByteArray(image)代替decodeFile(new String(image))。信息留待将来参考:

toString()不适用于数组(它返回一些随机字符串)。由于您将无效字符串传递给decodeImage,因此会返回null以表示未找到该字符串。因此,如果您在其上调用方法,则会生成NullPointerException。如果使用字符集对字节数组进行编码,而不是使用image.toString(),请使用正确解码字节数组的new String(image)

相关问题