Reputation: 1040
I have an image that is stored as a byte array (can also be accessed from Android directory as a .jpg) and I'm trying to get the width and height of the image. I've tried to use javax.imageio however Dalvik doesn't support this lib, so I've opted to use Bitmap. Can anyone help me out here? I keep getting Null Pointer Exceptions.
public int getWidth(byte[] image) throws IOException{
Bitmap bitmapImage = BitmapFactory.decodeFile(image.toString());
int imageWidth = bitmapImage.getWidth();
return imageWidth;
}
Cheers.
Upvotes: 2
Views: 3387
Reputation: 1988
You could try using decodeByteArray(image)
instead of decodeFile(new String(image))
. Info left for future reference:
toString()
does not work with arrays (it returns some random string of characters). Since you pass an invalid string to decodeImage
, it returns null
to indicate that it was not found. Therefore, if you invoke a method on it, it results in a NullPointerException
. If the byte array is encoded using a charset, instead of using image.toString()
, use new String(image)
which properly decodes the byte array.
Upvotes: 4