欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Matisse與Glide--java.lang.NoSuchMethodError:com.bumptech.glide.RequestManager.load

 更新時間:2021年08月19日 09:59:35   作者:bfther  
這篇文章主要介紹了在使用Matisse與glide4.0.0以及4.0.0之后的版本過程中,碰到該問題java.lang.NoSuchMethodError:com.bumptech.glide.RequestManager.load的解決方法

問題描述

在使用 Matisseglide 4.0.0 以及 4.0.0 之后的版本過程中,發(fā)現(xiàn)通過
Matisse 的 wiki 代碼調(diào)用選取圖片的方式后,會出現(xiàn)本文標題的錯誤
1.以下為 wiki 調(diào)用代碼:

Matisse.from(MainActivity.this)
        .choose(MimeType.allOf())
        .countable(true)
        .maxSelectable(9)
        .addFilter(new GifSizeFilter(320, 320, 5 * Filter.K * Filter.K))
        .gridExpectedSize(getResources().getDimensionPixelSize(R.dimen.grid_expected_size))
        .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
        .thumbnailScale(0.85f)
        .imageEngine(new GlideEngine())
        .forResult(REQUEST_CODE_CHOOSE);

2.以下為控制臺報出的錯誤信息,內(nèi)容過多,我就只截取了錯誤標題部分

java.lang.NoSuchMethodError: com.bumptech.glide.RequestManager.load
at com.zhihu.matisse.engine.impl.GlideEngine.loadThumbnail(GlideEngine.java:36)
at com.zhihu.matisse.internal.ui.widget.MediaGrid.setImage(MediaGrid.java:117)
at com.zhihu.matisse.internal.ui.widget.MediaGrid.bindMedia(MediaGrid.java:84)

問題定位

通過報錯信息,可以發(fā)現(xiàn)出現(xiàn)錯誤的調(diào)用路徑,根據(jù)路徑依次查看,發(fā)現(xiàn)在類 GlideEngine.java中,出現(xiàn)如下代碼:

    @Override
    public void loadThumbnail(Context context, int resize, Drawable placeholder, ImageView imageView, Uri uri) {
        Glide.with(context)
                .load(uri)
                .asBitmap()  // some .jpeg files are actually gif
                .placeholder(placeholder)
                .override(resize, resize)
                .centerCrop()
                .into(imageView);
    }

其中,有一行顯示為紅色,也就是說,該行代碼出現(xiàn)錯誤,導(dǎo)致程序的崩潰。

.asBitmap()

問題分析

在問題定位中,發(fā)現(xiàn) Matisse 在通過 Glide 調(diào)用相關(guān) api 的時候出現(xiàn)錯誤,因此,問題主要原因出現(xiàn)在 Glide 的使用上。通過查看 Glide 的 相關(guān)文檔,發(fā)現(xiàn) Glide 從 v3 到 v4 做了一些改動。
相關(guān)文檔 中發(fā)現(xiàn)

Glide.with(fragment)
    .asBitmap()
    .apply(myOptions)
    .load(url)
    .into(bitmapView);

仔細看這里的內(nèi)容,這里 .asBitmap() 的調(diào)用是在 .load(url) 之前,而我們在 問題定位 中的代碼, .asBitmap() 的調(diào)用是在 .load(url) 之后。隨后我在測試中,調(diào)換了兩者的位置,發(fā)現(xiàn) .asBitmap() .load(url)之前調(diào)用沒有問題,而在 .load(url) 之后調(diào)用,的確出現(xiàn)了本文所描述的問題。

問題解決

到此,問題的原因已經(jīng)很明確了。下面來談?wù)剢栴}的解決方式。

1.簡單粗暴型的解決方式,將代碼中的.imageEngine(new GlideEngine()) 替換成 .imageEngine(new PicassoEngine())(注:需要 Picasso 依賴)

Matisse.from(MainActivity.this)
        .choose(MimeType.allOf())
        .countable(true)
        .maxSelectable(9)
        .addFilter(new GifSizeFilter(320, 320, 5 * Filter.K * Filter.K))
        .gridExpectedSize(getResources().getDimensionPixelSize(R.dimen.grid_expected_size))
        .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
        .thumbnailScale(0.85f)
      //.imageEngine(new GlideEngine())
        .imageEngine(new PicassoEngine())
        .forResult(REQUEST_CODE_CHOOSE);

2.自定義類MyGlideEngine,仿照類 GlideEngine,重新實現(xiàn) ImageEngine,具體實現(xiàn)如下(注:該類未進行封裝,代碼不優(yōu)雅,各位可根據(jù)自己業(yè)務(wù)需求,進行相應(yīng)的修改優(yōu)化)

public class MyGlideEngine implements ImageEngine {

    @Override
    public void loadThumbnail(Context context, int resize, Drawable placeholder, ImageView imageView, Uri uri) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(placeholder)//這里可自己添加占位圖
                .error(R.drawable.error)//這里可自己添加出錯圖
                .override(resize, resize);
        Glide.with(context)
                .asBitmap()  // some .jpeg files are actually gif
                .load(uri)
                .apply(options)
                .into(imageView);
    }

    @Override
    public void loadGifThumbnail(Context context, int resize, Drawable placeholder, ImageView imageView,
                                 Uri uri) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(placeholder)//這里可自己添加占位圖
                .error(R.drawable.error)//這里可自己添加出錯圖
                .override(resize, resize);
        Glide.with(context)
                .asBitmap()
                .load(uri)
                .apply(options)
                .into(imageView);
    }

    @Override
    public void loadImage(Context context, int resizeX, int resizeY, ImageView imageView, Uri uri) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .override(resizeX, resizeY)
                .priority(Priority.HIGH);
        Glide.with(context)
                .load(uri)
                .apply(options)
                .into(imageView);
    }

    @Override
    public void loadGifImage(Context context, int resizeX, int resizeY, ImageView imageView, Uri uri) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .override(resizeX, resizeY)
                .priority(Priority.HIGH);
        Glide.with(context)
                .asGif()
                .load(uri)
                .apply(options)
                .into(imageView);
    }

    @Override
    public boolean supportAnimatedGif() {
        return true;
    }

}

自定義類寫好后,將 .imageEngine(new GlideEngine()) 修改成 .imageEngine(new MyGlideEngine ()),然后運行項目,查看能否正常使用。

總結(jié)

由于 Matisse 很久未更新維護,所以在 Glide 升級后,會出現(xiàn)這種問題。希望 Matisse 貢獻者在接下來的維護中,修復(fù)這個問題。
本方案在我目前的項目中可正常使用,如果各位沒有解決問題,還請勿噴。

到此這篇關(guān)于詳解Matisse與Glide--java.lang.NoSuchMethodError:com.bumptech.glide.RequestManager.load的文章就介紹到這了,更多相關(guān)詳解Matisse與Glide內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論