Android createBitmap导致ArrayIndexOutOfBounds

时间:2016-06-23 09:11:47

标签: android bitmap

我使用imageview显示Webp图片。首先,使用loadFileAsByteArray获取webp图片的ByteArray。然后,使用wepToBitmap获取Bitmap。行:Bitmap.createBitmap(pixels,width [0],height [0],Bitmap.Config.ARGB_8888);造成例外:

     Caused by: java.lang.ArrayIndexOutOfBoundsException
   at android.graphics.Bitmap.createBitmap(Bitmap.java:767)
   at android.graphics.Bitmap.createBitmap(Bitmap.java:796)
  at cn.library.util.BitmapUtil.webpToBitmap(BitmapUtil.java:717)

Fllowing是代码:

 public static Bitmap webpToBitmap(byte[] encoded) {
            int[] width = new int[] { 0 };
            int[] height = new int[] { 0 };
            byte[] decoded = libwebp.WebPDecodeARGB(encoded, encoded.length, width, height);
            int[] pixels = new int[decoded.length / 4];
            ByteBuffer.wrap(decoded).asIntBuffer().get(pixels);
            return Bitmap.createBitmap(pixels, width[0], height[0], Bitmap.Config.ARGB_8888);
        }

    public static byte[] loadFileAsByteArray(String filePath) {
        File file = new File(filePath);
        byte[] data = new byte[(int)file.length()];
        try {
            FileInputStream inputStream;
            inputStream = new FileInputStream(file);
            inputStream.read(data);
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

0 个答案:

没有答案