在URL上显示android上的WEBP图像

时间:2013-10-23 09:11:54

标签: java android webp

我正试图从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 ...

0 个答案:

没有答案