1

i need to implement textview with HTML, and normally images. I used code, that found here: Android HTML.fromHTML() with images?

URLDrawable.java

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

URLImageParser.java

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

    /***
     * Construct the URLImageParser which will execute AsyncTask and refresh the container
     * @param t
     * @param c
     */
    public URLImageParser(View t, Context c) {
        this.c = c;
        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();
        }
    }
}

Here I'm setting my textview text:

URLImageParser parser = new URLImageParser(textView, getActivity());
Spanned spanned = Html.fromHtml(myArticle.getDescription(), parser, null);
textView.setText(spanned);

But i'm getting images too small:

enter image description here

If i changing drawable.setBounds(0, 0, 500(just example), 500(just example))

I'm getting image with this problem:

enter image description here

I have just beginned my way in android, help me please.

Community
  • 1
  • 1
llaerto
  • 251
  • 1
  • 2
  • 11
  • 1
    Have you checked [WebView](http://developer.android.com/reference/android/webkit/WebView.html) yet? It looks like it is what you need (to render HTML) – Bonatti Sep 04 '15 at 11:39
  • you helped me a lot! It was so easy :( – llaerto Sep 04 '15 at 12:06
  • Glad to help. Welcome to stackoverflow, and good luck with your programs.. If you need help, always search the site, before asking questions...... Also, study the LogCat tool, to post the problems when asking questions. – Bonatti Sep 04 '15 at 12:13

1 Answers1

1

Thx to Bonatti's comments. It's best way to use WebView

llaerto
  • 251
  • 1
  • 2
  • 11
  • Glad to have helped. Just to inform, when a question is answered, and does not need further help, click the green V symbol on the left, to mark as finished. – Bonatti Sep 14 '15 at 11:32