[英]can't get the url of a image with jsoup
我正在尝试获取像这样的一系列图像的网址:
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);
}
但是每次我尝试运行我的应用程序时,都会收到以下警告:
java.net.MalformedURLException: Unknown protocol: data
at java.net.URL.<init>(URL.java:184)
at java.net.URL.<init>(URL.java:127)
所以我试图打印URL,我得到了:
data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
而且我可以找出原因,因为我100%确信我选择的元素是正确的,并且我对网站的其他部分进行了相同的处理,因此它可以正常工作。
更新1 :我尝试过这种方法来解码“ base64”图像:
byte[] decodedString = Base64.decode(imageMainUrl, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
但是结果是一样的。
这是数据URI方案
http://en.wikipedia.org/wiki/Data_URI_scheme
它允许在您的URI中添加内联数据。
parseBase64Binary
获取字节数组, parseBase64Binary
更多信息,请parseBase64Binary
: http : parseBase64Binary
.lang.String%29 ByteArrayInputStream
BitmapFactory
将其解码为Bitmap
编辑
此代码有效,它给出了1px * 1px gif图像。 我从commons-codec
使用org.apache.commons.codec.binary.Base64
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.