我正在尝试从Firebase Storage
加载StorageReference
。我正在使用Glide
库和FirebaseUI
的FirebaseImageLoader
类。.using 方法解析失败。我认为问题与上下文有关。homeTeam是我的ImageView。
public class MatchAdapter extends FirestoreAdapter<MatchAdapter.ViewHolder> {
//......
static class ViewHolder extends RecyclerView.ViewHolder {
private Context mContext;
@BindView(R.id.imageView)
ImageView homeTeam;
@BindView(R.id.imageView2)
ImageView awayTeam;
@BindView(R.id.textView)
TextView score;
@BindView(R.id.textView2)
TextView competition;
@BindView(R.id.textView3)
TextView game;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
public void bind(final DocumentSnapshot snapshot,
final OnMatchSelectedListener listener) {
Match match = snapshot.toObject(Match.class);
Resources resources = itemView.getResources();
Glide.with(mContext)
.using(new FirebaseImageLoader())
.load(match.getHomeTeam())
.into(homeTeam);
Glide.with(awayTeam.getContext())
.load(match.getAwayTeam())
.into(awayTeam);
score.setText(match.getScore());
competition.setText(match.getCompetition());
game.setText(match.getGame());
// Click listener
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener != null) {
listener.onMatchSelected(snapshot);
}
}
});
}
}
// ...
}
看起来您正在使用Glide 4。
您需要创建一个扩展AppGlideModule
的类
@GlideModule
public class MyAppGlideModule extends AppGlideModule {
@Override
public void registerComponents(Context context, Glide glide, Registry registry) {
// Register FirebaseImageLoader to handle StorageReference
registry.append(StorageReference.class, InputStream.class,
new FirebaseImageLoader.Factory());
}
}
然后您可以使用 GlideApp 像往常一样加载图像。
GlideApp.with(this /* context */)
.load(storageReference)
.into(imageView);
阅读 FirebaseUI 存储自述文件,了解更多信息
希望这对:)有所帮助