Android应用程序显示图像从网址 [英] Android App Display Image From URL
问题描述
我在寻找一个非常基本的功能。我试图设计一个应用程序,所有我希望它做的是从URL加载图像。 我发现了几个问题和网站,但他们都似乎是老年人和过时,但我觉得我在与被链接的code到活动main.xml中的ImageView的问题。
I'm looking for a very basic function. I'm trying to design an app and all I want it to do is load an image from a URL. I've found a few questions and websites, but they all seem to be older and dated, but what I think I'm having issues with is linking the code to activity main.xml for ImageView.
任何建议或链接你,我将不胜AP preciate,谢谢你。
Any suggestions or links you have I would greatly appreciate, thank you.
推荐答案
在这里,这是我如何从URL中的图像视图
显示图像
您必须调用从线程比主线程
Here, this is how I display image from url in the image view
you have to call this code from thread other than main thread
ImageView img = (ImageView) findViewById(R.id.imageView1);
try {
URL url = new URL("Your URL");
//try this url = "http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg"
HttpGet httpRequest = null;
httpRequest = new HttpGet(url.toURI());
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient
.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
InputStream input = b_entity.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(input);
img.setImageBitmap(bitmap);
} catch (Exception ex) {
}
要小心不要忘了周围的code与尝试catch(我已经做了,在这个code)
Be careful don't forget to surround the code with try catch(I have already done that in this code)
也可以使用web视图加载从URL图像
or you can use webview to load image from url
WebView web = (WebView) findViewById(R.id.webView1);
web.loadUrl("Your Url");
如果你要加载的资源文件夹URL的图像将这样开始
文件:///android_asset/yourimage.jpg
其他正常的互联网网址类似这样的
<一href="http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg">http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg"
if you are trying to load image from the assets folder url will start like this
"file:///android_asset/yourimage.jpg"
else normal internet url like this
"http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg"
希望这对你的作品 好运
hope this works for you Good Luck
这篇关于Android应用程序显示图像从网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!