从网站提取图像

时间:2011-04-28 05:57:12

标签: android

如何从网站中提取图像并在网格或列表视图中显示?

4 个答案:

答案 0 :(得分:2)

Bitmap bmImg;
    void downloadfile(String fileurl,ImageView img)
    {
        URL myfileurl =null;
        try
        {
            myfileurl= new URL(fileurl);

        }
        catch (MalformedURLException e)
        {

            e.printStackTrace();
        }

        try
        {
            HttpURLConnection conn= (HttpURLConnection)myfileurl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            int length = conn.getContentLength();
            int[] bitmapData =new int[length];
            byte[] bitmapData2 =new byte[length];
            InputStream is = conn.getInputStream();
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 
            bmImg = BitmapFactory.decodeStream(is,null,options);

            img.setImageBitmap(bmImg);

            //dialog.dismiss();
            } 
        catch(IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
//          Toast.makeText(PhotoRating.this, "Connection Problem. Try Again.", Toast.LENGTH_SHORT).show();
        }


    }

试试这个

答案 1 :(得分:0)

url解释了如何使用异步任务从远程服务器下载图像并在列表视图中显示下载的图像。

答案 2 :(得分:0)

下面的代码可以帮助您获取图像。如果您传递图像的网址。

      public Drawable loadImageFromWebOperations(String url)
{

     try
     {

         DefaultHttpClient client = new DefaultHttpClient();
         HttpGet httpGetRequest = new HttpGet(url);  
        HttpResponse response = client.execute(httpGetRequest);  
        InputStream is = response.getEntity().getContent();


         Drawable d = Drawable.createFromStream(is, "src name");

         return d;

     }   catch (Exception e) {

         System.out.println("Exc="+e);

         return null;

     }

 }

此代码将返回一个可在ImageView中显示的drawable

      imageView.setImageDrawable(d);

您可以将ImageView添加到List的每一行并设置drawable。

答案 3 :(得分:0)

如果你想解析整个网站,我更喜欢使用Jerico HTML Parser,它适用于Android(尝试过),然后你可以搜索<img>元素来获取图片网址。

相关问题