Android: Textview imageGetter 显示html 内容和图片。 ImageGetter的缓存图片

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

我是 Android 编程新手。目前,我正在开发一个应用程序,通过使用 ImageGetter 和 Html.fromHtml() 在 TextView 中显示 HTML 内容。是否可以将内容和图像缓存在 TextView 中,以便我可以离线查看图像?

好吧,几天前我也在做同样的事情。我正在使用 ImageGetter 显示 html 和 textview。但它没有给出建议的结果。然后我使用 Jsoup libaray 并提取元素并将它们显示在 Textvew 中,当 img 标签出现时,我使用 UIL 通用图像库在 Imageview 中显示图像。那么这个 libaray 有缓存图像的选项。 您只需要缓存 html 字符串,然后将其传递给下面的 class 以及布局 rest jsoup 和 UIL 将处理。

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

这是我的代码。我尝试调试,它显示代码进入 onLoadingFailed() 而没有进入 onLoadingComplete()。我不知道为什么。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    linearLayout = (LinearLayout)findViewById(R.id.linearLayout1);
     DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheOnDisc(true).cacheInMemory(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .displayer(new FadeInBitmapDisplayer(100)).build();

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
            getApplicationContext())
            .defaultDisplayImageOptions(defaultOptions)
            .memoryCache(new WeakMemoryCache())
            .discCacheSize(10 * 1024 * 1024).build();

    ImageLoader.getInstance().init(config);
    setContentToView();
}

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(this);
            textView.setText(Html.fromHtml(heading));
            textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
            linearLayout.addView(textView);
        }


        if(tag.getName().equalsIgnoreCase("p")){
            element.select("img").remove();
            String body= element.html();
            TextView textView = new TextView(this);
            textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
            textView.setText(Html.fromHtml(body));
            this.linearLayout.addView(textView);
            p.add(body);
        }


        if (tag.getName().equalsIgnoreCase("img")){
            String url  = element.select("img").attr("src");
            final ImageView imageView = new ImageView(this);
            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 onLoadingStarted(String imageUri, View view) {
                    super.onLoadingStarted(imageUri, view);

                }

                @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 onLoadingCancelled(String imageUri, View view) {
                    super.onLoadingCancelled(imageUri, view);
                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    super.onLoadingFailed(imageUri, view, failReason);
                }
            });

            linearLayout.addView(imageView);
            src.add(url);
        }
    }
}

public static int getScreenWidth() {
    return Resources.getSystem().getDisplayMetrics().widthPixels;
}