Android 如何从非定向URL下载图像
使用Android 如何从非定向URL下载图像,android,image,Android,Image,使用Android Studio我想从URL下载图像,我的意思是这些图像都在这个URL中。我有代码,但是是直接下载的。 我必须在自定义列表视图中显示它们。 代码如下: ImageView img = (ImageView) rowView.findViewById(R.id.enclosure); try { URL url = new URL("MY_URL"); <-- the URL where I want to get the images in encl
Android Studio
我想从URL
下载图像
,我的意思是这些图像
都在这个URL中。我有代码,但是是直接下载的。
我必须在自定义列表视图中显示它们。
代码如下:
ImageView img = (ImageView) rowView.findViewById(R.id.enclosure);
try {
URL url = new URL("MY_URL"); <-- the URL where I want to get the images in enclosures
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) {
}
但是没有显示
图像。据我所知,这是在尝试解码整个页面的输入流,这不起作用,因为页面不仅仅是一个图像;还有文本等等。这行不通。您必须获取要下载的每个图像的URL,然后分别下载这些图像。因此,首先我必须获取URL图像的字符串,然后按字符串下载它们。我非常感谢您的帮助:)请原谅我的混乱,但没有人能回答我的另一个问题,可以吗?由于我必须获取这些URL的字符串
,我创建了另一个文本视图
,然后替换URL URL=新URL(“我的URL”)
I putURL URL=newurl(context.getString(R.id.my_new_textview))代码>。这是正确的吗?因为没有显示图像。为什么要尝试在文本视图中显示图像?你放的东西只会显示。。。嗯,我不知道是什么。你的资源指向一个ID,所以我不知道会发生什么。无论如何,第一件事是:您需要使用ImageView。这是你需要知道的一个基本概念。我仍然在使用上面的代码,但只在URL=newurl(“MY_-URL”)
I putURL URL=newurl(context.getString(R.id.my_new_textview))代码>因为要显示图像
,图像视图
必须参考URL
。我错了吗?如果不是,我还能做什么?
ImageView img = (ImageView) rowView.findViewById(R.id.enclosure);
img.setImageDrawable(web.get(position).getEnclosure());