Search code examples
androidimagetextviewbrowser-cache

Android: Textview imageGetter display html content and image. Cache image of ImageGetter


I am new in Android Programming. Currently, I am developed an application to display HTML content in TextView by using ImageGetter and Html.fromHtml(). Is it possible for me to cache the content and image in TextView in order I can view the image offline?


Solution

  • Well, Some days ago i was doing the same. I was displaying html with textview using ImageGetter. But it wasn't giving the proposed result. So then i used Jsoup libaray and i extract the elements and display them in Textvew and when img tag comes i display the image in Imageview using UIL universal image libarary. Well this libaray has Option of cacheing the images. You only need to cache the html string then pass it to the folllowing class along with layout rest jsoup and UIL will handle.

    public class PostContentHandler {
    Context context;
    String  content;
    LinearLayout linearLayout;
    public PostContentHandler(Context context, String content , LinearLayout linearLayout){
        this.context=context;
        this.content=content;
        this.linearLayout=linearLayout;
    
    }
    
    public void setContentToView(){
        List<String> p = new ArrayList<>();
        List<String> src = new ArrayList<>();
        Document doc = Jsoup.parse(content);
    
        Elements elements = doc.getAllElements();
    
        for(Element element :elements){
            Tag tag = element.tag();
            if(tag.getName().matches("h[1-6]{1}")){
                String heading = element.select(tag.getName().toString()).text();
                TextView textView = new TextView(context);
                textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT));
                textView.setTextSize(20);
                textView.setText(heading);
                textView.setPadding(5, 0, 5, 0);
                textView.setTextColor(R.color.black);
                linearLayout.addView(textView);
            }
    
            if(tag.getName().equalsIgnoreCase("p")){
                element.select("img").remove();
                String body= element.html();
                TextView textView = new TextView(context);
                textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT));
                textView.setText(Html.fromHtml(body));
                textView.setTextColor(R.color.black);
                textView.setPadding(10,0,10,0);
                linearLayout.addView(textView);
                p.add(body);
            }
            if (tag.getName().equalsIgnoreCase("img")){
                String url  = element.select("img").attr("src");
    
    
                final ImageView imageView = new ImageView(context);
                imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT));
                ImageLoader imageLoader = ImageLoader.getInstance();
                imageLoader.displayImage(url, imageView, new SimpleImageLoadingListener() {
                    @Override
                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                        super.onLoadingComplete(imageUri, view, loadedImage);
                        int height=loadedImage.getHeight();
                        imageView.getLayoutParams().height=getScreenWidth();
                        imageView.setAdjustViewBounds(true);
                        imageView.requestLayout();
    
                    }
    
                    @Override
                    public void onLoadingStarted(String imageUri, View view) {
                        super.onLoadingStarted(imageUri, view);
                    }
                });
    
                linearLayout.addView(imageView);
                src.add(url);
            }
    
        }
    }
    public static int getScreenWidth() {
        return Resources.getSystem().getDisplayMetrics().widthPixels;
    }
    }