0

I have some data in the html format. I am using Html.fromHtml(String) when setting the data to the textview in android. Also the html data contains 2 to 3 images. I want to get the name of the images from the html data and store them in an array. Based on the number of the images in the array I will set them to the imageview. How can I get the image name from src in image tag?

Which one will be the better option .i.e using regular expression or substring?

Please suggest some solutions and help me with some examples.

My Code:

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();
            }
        }
    }
Amrutha
  • 575
  • 4
  • 9
  • 29

2 Answers2

0

Html.ImageGetter() will be more helpful, it will find the tag <img> http://developer.android.com/reference/android/text/Html.ImageGetter.html

Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
  • Hi thanks for the reply...I have gone through Html.imagegetter examples. All of those I found getting the image from drawable. Actually all my images are stores in server becoz they are of large size. So now I need to get those images asynchronously from server and set them to textview using imagegetter method. How can I achieve this task? – Amrutha Jun 14 '12 at 05:51
  • you can do that by append server prefix filename `xxxx.imagefile.png` create image in `doInBackground`. – Mohammed Azharuddin Shaikh Jun 14 '12 at 05:55
  • @user1455252 Refer http://stackoverflow.com/questions/7424512/android-html-imagegetter-as-asynctask – Mohammed Azharuddin Shaikh Jun 14 '12 at 06:00
  • Hi I have gone through the link http://stackoverflow.com/questions/7424512/android-html-imagegetter-as-asynctask and done same like that. But instead of Context in URLImageParser class constructor I kept ListAdapter. Its because I am setting the text in the listview and so I created the object for the class in getview method. But finally the image is not displaying, only the test is displaying. Please suggest me where I went wrong..I kept my code..please check once. – Amrutha Jun 14 '12 at 09:18
0

I want to get the name of the images

If you are using jsoup html parser then life is much easier.

I dont know how images names are apperaing in HTML data.

But suppose your html has images as follows

<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

I have posted example snippet here

Community
  • 1
  • 1
Vipul
  • 27,808
  • 7
  • 60
  • 75