簡體   English   中英

從 TextView (Kotlin) 中的 html 內容下載和緩存內聯圖像的現代方法

[英]Modern way to download and caching inline images from html content inside TextView (Kotlin)

根據這個問題,我使用以下兩個類來下載里面的圖像(HTML內容<img標簽),但由於某些原因效率不高,首先有時它不是緩存所有圖像,其次static Picasso字段導致泄漏該應用程序,所以我正在尋找更好的方法並肯定支持高嶺土

畢加索緩存

public class PicassoCache {

//    @SuppressLint("StaticFieldLeak")
    private static Picasso picassoInstance = null;

    private PicassoCache(Context context) {

        Downloader downloader = new OkHttp3Downloader(context, Integer.MAX_VALUE);
        Picasso.Builder builder = new Picasso.Builder(context);
        builder.downloader(downloader);
        picassoInstance = builder.build();
    }

    public static Picasso getPicassoInstance(Context context) {

        if (picassoInstance == null) {

            //noinspection InstantiationOfUtilityClass
            new PicassoCache(context);
            return picassoInstance;
        }

        return picassoInstance;
    }
}

畢加索ImageGetter


public class PicassoImageGetter implements Html.ImageGetter {

    private static final String TAG = "PicassoImageGetter";

    private TextView textView = null;
    Context mContext;

    public PicassoImageGetter(TextView target, Context context) {
        textView = target;
        mContext = context;
    }

    public PicassoImageGetter() {

    }

    @Override
    public Drawable getDrawable(String source) {
        BitmapDrawablePlaceHolder drawable = new BitmapDrawablePlaceHolder();
//        Picasso.get().load(source).into(drawable);
        PicassoCache.getPicassoInstance(mContext)
                .load(source)
                .priority(Picasso.Priority.HIGH)
                .error(R.drawable.no_image)
                .into(drawable);

        return drawable;

    }

    @SuppressWarnings("deprecation")
    private class BitmapDrawablePlaceHolder extends BitmapDrawable implements com.squareup.picasso.Target {
        protected Drawable drawable;
        @Override
        public void draw(final Canvas canvas) {
            if (drawable != null) {
                drawable.draw(canvas);
            }
        }

        public void setDrawable(Drawable drawable) {
                this.drawable = drawable;
                int width = drawable.getIntrinsicWidth();
                int height = drawable.getIntrinsicHeight();
                drawable.setBounds(0, 0, width, height);
                setBounds(0, 0, width, height);
            if (textView != null) {
                textView.setText(textView.getText());
            }
        }

        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            setDrawable(new BitmapDrawable(mContext.getResources(), bitmap));
        }

        @Override
        public void onBitmapFailed(Exception e, Drawable errorDrawable) {
            setDrawable(errorDrawable);
            Log.e(TAG, "onBitmapFailed: "+e.toString() );
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }

    }
}

我像這樣使用它

val imageGetter = PicassoImageGetter(binding.blogContent, this)
        val html: Spannable = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY, imageGetter, null) as Spannable
        } else {
            Html.fromHtml(content, imageGetter, null) as Spannable
        }
        binding.blogContent.text = html

結果

結果

我發現這個庫html-textview sufficientlysecure.htmltextview.HtmlTextView secure.htmltextview.HtmlTextView

依賴

在項目 Gradle 文件中:

repositories {
    jcenter()
}

在應用程序 Gradle 文件中:

dependencies {
implementation 'org.sufficientlysecure:html-textview:3.9'
}

在 XML 文件中,將您的 textView 替換為:

<org.sufficientlysecure.htmltextview.HtmlTextView
      android:id="@+id/allNewsBlockTextView"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_margin="2dp"
      android:textColor="#000"
      android:textSize="18sp"
      app:htmlToString="@{detailsViewModel.selectedText}" />

有一個問題項目已停止。 4.0 是最后一個版本,沒有更多更新/維護,我會一直使用它,直到找到更好的解決方案

看看這個問題的公認答案,關於如何替換由HtmlHtmlCompat處理的 HTML 字符串中的占位符。 (授予賞金的答案中的示例項目有一個稍微改進的解決方案。)您需要用另一個實際進行網絡獲取的simulateNetworkFetch() function 替換。 (這只是答案的演示。)

為了有效地從網絡中獲取圖像,我建議您查看Glide 庫 網上有關於如何在項目中實現Glide的教程。 我確信畢加索有類似的功能,但我沒有任何經驗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM