从Android的URL显示图像 [英] Android display image from URL
问题描述
我怎样才能从下面网址加载图像。
http://images.google.com/hosted/life/l?imgurl=46d75bbfa52a8450
How can I load image from below url . http://images.google.com/hosted/life/l?imgurl=46d75bbfa52a8450
我使用谷歌自定义搜索API从网站 images.google.com 搜索图像。
它会返回一个链接标签内容的JSON文件。例如: http://images.google.com/hosted/life/l?imgurl = 46d75bbfa52a8450 。我想加载到我的看法。
I use Google Custom Search API to search image from site images.google.com . It will return a json file with contents of link tag . Example : http://images.google.com/hosted/life/l?imgurl=46d75bbfa52a8450 . And I want to load it to my view .
如果URL是ENDWITH文件扩展.jpg或.png,我可以下载并显示它。
但如果不是这样,我可以得到图像。
任何人都可以帮助我。
If URL is endwith file extends ".jpg or .png", i can download and display it . But if not , i can get the image . Anyone can help me .
推荐答案
喜请使用以下code。
Hi Please used below code.
String url = "http://images.google.com/hosted/life/l?imgurl=46d75bbfa52a8450";
InputStream ins = null;
try {
ins = new java.net.URL(url).openStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Bitmap b = BitmapFactory.decodeStream(new FlushedInputStream(ins));
imageview.setImageBitmap(b);
static class FlushedInputStream extends FilterInputStream {
public FlushedInputStream(InputStream inputStream) {
super(inputStream);
}
@Override
public long skip(long n) throws IOException {
long totalBytesSkipped = 0L;
while (totalBytesSkipped < n) {
long bytesSkipped = in.skip(n - totalBytesSkipped);
if (bytesSkipped == 0L) {
int b = read();
if (b < 0) {
break; // we reached EOF
} else {
bytesSkipped = 1; // we read one byte
}
}
totalBytesSkipped += bytesSkipped;
}
return totalBytesSkipped;
}
}
这篇关于从Android的URL显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!