Android: Replace <img>-tag with ImageViews

守給你的承諾、 提交于 2019-12-01 08:07:39

问题


I'm showing a bit of HTML in a TextView using Html.fromHtml, which works great.

Sometimes this html will have img-tags, and I'd also like to display these.

I tried Android HTML ImageGetter as AsyncTask but this seemed slow and unpredictable (size of the image).

Example HTML (it can differ...):

<h2>title</h2><br /><p>TEXT</p>
<p style="text-align: center;">Anyways, fokea?.<img src="http://xxxximages/1048268-11-1426170876314.jpg" alt="" /><br /><br /><br /></p>
<p>Jo, veldig mye blomster og duftelys. elt feil sk. Luksus!</p>
<p style="text-align: center;"><img src="http://xxxx/images/1048268-11-1426171000272.jpg" alt="" /></p>
<p><strong>Håper dere har det hakket bedre enn meg.</strong> </p>

I thought about using JSOUP to get all img-tags, and then create X amounts of ImageViews (and populating them with Picasso). This works OK, but the images are always either on top or the bottom of text.

What would be the best solution for replacing every img-tag and for each one, at the correct place according to the text, create a new ImageView?

EDIT: Well, since no-one had any suggestions, I made this quick and dirty one.

ArrayList<String> lines = new ArrayList<>();
    Scanner scanner = new Scanner(content);
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        lines.add(line);
    }
    scanner.close();


 for(int i = 0; i < lines.size(); i++) {
        Document doc = Jsoup.parse(lines.get(i));
        Elements imgs = doc.select("img");
        if(imgs.size() == 0) {
            TextView textView = new TextView(this);
            textView.setText(Html.fromHtml(lines.get(i)));
            maincontainer.addView(textView);
        } else {
            for(Element el : imgs) {
                Element img = el.select("img").first();
                String image = img.absUrl("src");
                ImageView imageView = new ImageView(this);
                imageView.setPadding(0, 10, 0, 10);
                imageView.setAdjustViewBounds(true);
                Picasso.with(getApplicationContext()).load(image).into(imageView);
                maincontainer.addView(imageView);
            }
        }
    }

It looks and works fantastic, although I'm sure the code is far from optimal.


回答1:


I solved it using this:

ArrayList<String> lines = new ArrayList<>();
Scanner scanner = new Scanner(content);
while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    lines.add(line);
}
scanner.close();

for(int i = 0; i < lines.size(); i++) {
    Document doc = Jsoup.parse(lines.get(i));
    Elements imgs = doc.select("img");
    if(imgs.size() == 0) {
        TextView textView = new TextView(this);
        textView.setText(Html.fromHtml(lines.get(i)));
        maincontainer.addView(textView);
    } else {
        for(Element el : imgs) {
            Element img = el.select("img").first();
            String image = img.absUrl("src");
            ImageView imageView = new ImageView(this);
            imageView.setPadding(0, 10, 0, 10);
            imageView.setAdjustViewBounds(true);
            Picasso.with(getApplicationContext()).load(image).into(imageView);
            maincontainer.addView(imageView);
        }
    }
}


来源:https://stackoverflow.com/questions/29051009/android-replace-img-tag-with-imageviews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!