简体   繁体   中英

Android: Get height/width from byte array or file

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM