javaandroidjsoupmalformedurlexception

can't get the url of a image with jsoup


I'm trying to get the url of a range of images like that:

for(Element img : document.select(".left-column .strillo-content .lazy img[src]")) {
    InputStream input = new java.net.URL(imageMainUrl).openStream();
    Bitmap bitmap = BitmapFactory.decodeStream(input);
    images.add(bitmap);
}

but everytime I trying to run my app I get this warning:

java.net.MalformedURLException: Unknown protocol: data
at java.net.URL.<init>(URL.java:184)
at java.net.URL.<init>(URL.java:127)

so I have tried to print the URL and I get this:

data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7

and I can figure out why, because I'm 100% sure that the element I select is corrent and also I do the same process with other section of the website and it works..

UPDATE 1: I have tried this method to decode the ´base64´ image:

byte[] decodedString = Base64.decode(imageMainUrl, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

but the result is the same..


Solution

  • It's the data URI scheme

    http://en.wikipedia.org/wiki/Data_URI_scheme

    It allows to add inline data in your URI.

    edit

    This code works, it give a 1px*1px gif image. I used org.apache.commons.codec.binary.Base64 from commons-codec

    String uri = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
    byte[] decodedString = Base64.decodeBase64(uri.substring(uri.indexOf("data:image/gif;base64,") + "data:image/gif;base64,".length()));
    ByteArrayInputStream is = new ByteArrayInputStream(decodedString);
    FileOutputStream os = new FileOutputStream(new File("/tmp/test.gif"));
    
    byte[] buffer = new byte[1024];
    int length;
    
    // copy the file content in bytes 
    while ((length = is.read(buffer)) > 0)
    {
        os.write(buffer, 0, length);
    }
    
    is.close();
    os.close();