从android中的整个字符串中检索特定单词?

时间:2012-06-14 04:02:09

标签: android regex

我有一些html格式的数据。在将数据设置为android中的textview时,我正在使用Html.fromHtml(String)。此外,html数据包含2到3个图像。我想从html数据中获取图像的名称并将它们存储在一个数组中。根据数组中图像的数量,我将它们设置为imageview。如何从图像标签中的src获取图像名称?

哪一个是更好的选择.i.e使用正则表达式还是子串?

请提供一些解决方案,并帮我提供一些示例。

我的代码:

public View getView(int position, View convertView, ViewGroup parent) 
{ 
---
---
   desc = (TextView) view.findViewById(R.id.description);
   URLImageParser p = new URLImageParser(desc, this);
   Spanned htmlSpan = Html.fromHtml(listItem.getdesc(), p, null);
   desc.setText(htmlSpan);
----
----
----
----
}


public class URLDrawable extends BitmapDrawable {
        // the drawable that you need to set, you could set the initial drawing
        // with the loading image if you need to
        protected Drawable drawable;

        @Override
        public void draw(Canvas canvas) {
            // override the draw to facilitate refresh function later
            if(drawable != null) {
                drawable.draw(canvas);
            }
        }
    }



    public class URLImageParser implements ImageGetter {
        ListAdapter c;
        View container;

        /***
         * Construct the URLImageParser which will execute AsyncTask and refresh the container
         * @param t
         * @param listAdapter
         */
        public URLImageParser(View t, ListAdapter listAdapter) {
            this.c = listAdapter;
            this.container = t;
        }

        public Drawable getDrawable(String source) {
            URLDrawable urlDrawable = new URLDrawable();

            // get the actual source
            ImageGetterAsyncTask asyncTask = 
                new ImageGetterAsyncTask( urlDrawable);

            asyncTask.execute(source);

            // return reference to URLDrawable where I will change with actual image from
            // the src tag
            return urlDrawable;
        }

        public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable>  {
            URLDrawable urlDrawable;

            public ImageGetterAsyncTask(URLDrawable d) {
                this.urlDrawable = d;
            }

            @Override
            protected Drawable doInBackground(String... params) {
                String source = params[0];
                return fetchDrawable(source);
            }

            @Override
            protected void onPostExecute(Drawable result) {
                // set the correct bound according to the result from HTTP call
                urlDrawable.setBounds(0, 0, 0 + result.getIntrinsicWidth(), 0 
                        + result.getIntrinsicHeight()); 

                // change the reference of the current drawable to the result
                // from the HTTP call
                urlDrawable.drawable = result;

                // redraw the image by invalidating the container
                URLImageParser.this.container.invalidate();
            }

            /***
             * Get the Drawable from URL
             * @param urlString
             * @return
             */
            public Drawable fetchDrawable(String urlString) {
                try {
                    InputStream is = fetch(urlString);
                    Drawable drawable = Drawable.createFromStream(is, "src");
                    drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0 
                            + drawable.getIntrinsicHeight()); 
                    return drawable;
                } catch (Exception e) {
                    return null;
                } 
            }

            private InputStream fetch(String urlString) throws MalformedURLException, IOException {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet request = new HttpGet(urlString);
                HttpResponse response = httpClient.execute(request);
                return response.getEntity().getContent();
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

Html.ImageGetter()会更有帮助,它会找到标记<img> http://developer.android.com/reference/android/text/Html.ImageGetter.html

答案 1 :(得分:0)

  

我想获得图片的名称

如果您正在使用jsoup html解析器,那么生活会更容易。

我不知道图像名称在HTML数据中是如何出现的。

但是假设你的html图像如下

<img src='image_url/abc.png'>

File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");

Elements pngs = doc.select("img[src$=.png]").first();

String imageurl= pngs.attr("src"); which will return 
  

IMAGE_URL / abc.png

我已发布示例代码段here