0

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

哪一个是更好的选择。即使用正则表达式或子字符串?

请提出一些解决方案并帮助我举一些例子。

我的代码:

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();
            }
        }
    }
4

2 回答 2

0

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

于 2012-06-14T04:10:39.963 回答
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

我在这里发布了示例片段

于 2012-06-14T04:13:11.740 回答