0

我正在尝试从 Google PlayStore 获取并显示一个 WEBP 图标,如下所示:http: //lh5.ggpht.com/YK0as9_ZRmSK6PBkSwRVQ62F5cBMq7gYMLfZ2nk30QjEVK6taYMPMKzh0eswMkBMhw=w170-rw

我尝试直接使用数据并使用 BitmapFactory 对其进行解码(有时可以...),我也尝试使用 libwebp,但是当我使用“WebPGetInfo”函数测试数据时,它返回 0(这意味着数据不是 webp image 。 ..)

这是我与 libwebp 一起使用的代码:

public static Bitmap getBitmapFromURL(String src) {
        HttpURLConnection connection = null;
        Bitmap bmp = null;
        try {
            connection = (HttpURLConnection) new URL(src).openConnection();
            connection.setRequestMethod("GET");
            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", 
                   "image/webp");
            connection.connect();

          //Send request
              DataOutputStream wr = new DataOutputStream (
                          connection.getOutputStream ());
              wr.writeBytes ("");
              wr.flush ();
              wr.close ();

              //Get Response    
              BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            byte[] buf = new byte[4096];
            while(true) {
                String n = rd.readLine();
                if(n == null)break;
                baos.write(n.getBytes(), 0, n.getBytes().length);
                baos.write('\n');
            }

            byte data[] = baos.toByteArray();
                    // From the lib's exemple 
            int[] width = new int[] { 0 };
            int[] height = new int[] { 0 };

            int test = libwebp.WebPGetInfo(data, data.length, width, height); // test = 0 ! ! !

            byte[] decoded = libwebp.WebPDecodeARGB(data, data.length, width, height); 

            int[] pixels = new int[decoded.length / 4];
            ByteBuffer.wrap(decoded).asIntBuffer().get(pixels);

            bmp = Bitmap.createBitmap(pixels, width[0], height[0], Bitmap.Config.ARGB_8888);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bmp;

    }

谢谢你的帮助。

编辑:经过多次更改后,我现在在使用 libwebp 时出现 NoClassDefFoundError ...

4

0 回答 0