1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
   | 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;
 
		} | 
Partager