使用Glide加载资源ID返回noModelLoaderAvailableException

问题描述 投票:0回答:1

据我所知,通过资源ID加载图像的方式与通常从Internet加载方式相同:

Glide.with(fragmentContext).load(R.id.drawable).into(imageView);

(尽管我的加载方式略有不同,因为我有动态资源图像)

Glide.with(fragmentContext).load(R.drawable.class.getField("image_path_" + model.modelID))
    .into(new CustomTarget<Drawable>() {
        @Override
        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
            textView.setBackground(resource);
        }

        @Override
        public void onLoadCleared(@Nullable Drawable placeholder) {
            textView.setBackground(placeholder);
        }
    });

我要加载的图像是保存在drawable-nodpi文件夹中的.jpg。

[尝试在回收器适配器的onBindViewHolder中加载图像时,出现以下错误:

    com.bumptech.glide.Registry$NoModelLoaderAvailableException: Failed to find any ModelLoaders for model: public static final int com.example.me.project.R$drawable.image

这与我的图像格式有关,还是与它存储在资产/资源文件夹中的位置有关?我不认为我必须为如此简单的任务实现复杂的模型加载器。

android android-recyclerview android-resources android-glide
1个回答
0
投票

尽管getField()代码段在设置View元素的资源时有效,但不适用于Glide,因为它特别期望resource Id,即int。键入Field!= Int

在这种情况下,您必须从资源中显式获取Int资源ID。

这里将是与glide的图像加载器一起使用的更新代码:

R.drawable.class.getField("name").getInt(null)

© www.soinside.com 2019 - 2024. All rights reserved.