如何从有向URL下载图像

时间:2014-01-07 22:35:49

标签: android image

使用Android Studio我想从images下载URL,我的意思是这些images位于此网址中。我有代码,但它是直接下载它们。 我必须在Custom ListView中展示它们。 这是代码:

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) {

    }

在我这之前:

ImageView img = (ImageView) rowView.findViewById(R.id.enclosure);
img.setImageDrawable(web.get(position).getEnclosure());

但未显示images

1 个答案:

答案 0 :(得分:0)

据我所知,这是尝试解码整个页面的输入流,这不起作用,因为页面不仅仅是一个图像;有文字等等。这不行。您必须获取要下载的每个图像的URL,然后单独下载这些图像。