Android: Replace <img>-tag with ImageViews

浪子不回头ぞ 提交于 2019-12-01 11:10:54

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