Я пытаюсь загрузить изображения в RecyclerView с помощью Glide, но я получаю сообщение об ошибке, Glide не удалось загрузить ресурс, даже если URL-адрес изображения, кажется, там, и когда я нажимаю на URL-адрес, я могу просмотреть изображение .
Вот мой код FiltersViewHolder;
public class FiltersViewHolder extends RecyclerView.ViewHolder {
public static String PHOTO_URL="";
private Context context;
TrendingFilters trendingFilters;
@BindView(R.id.filters_image)
ImageView filtersImage;
public FiltersViewHolder(Context context, View itemView) {
super(itemView);
this.context = context;
ButterKnife.bind(this, itemView);
}
Здесь я пытаюсь привязать изображения к ImageView с помощью Glide;
public void bind(final TrendingFilters trendingFilters) {
this.trendingFilters = trendingFilters;
DiskCacheStrategy strategy = DiskCacheStrategy.NONE;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(context.getResources(), R.id.filters_image, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
Log.e("Bitmap", imageHeight + "" + imageWidth);
String imageType = options.outMimeType;
if(trendingFilters.getEmbedUrl() != null) {
GlideApp.with(context)
.load(Uri.parse(trendingFilters.getEmbedUrl()))
.placeholder(R.drawable.fox_face_mesh_texture)
.apply(RequestOptions.diskCacheStrategyOf(strategy))
.fitCenter()
.dontAnimate()
.into(filtersImage);
filtersImage.setImageBitmap(
decodeSampledBitmapFromResource(context.getResources(), R.id.filters_image, 100, 100));
PHOTO_URL = trendingFilters.getEmbedUrl();
}else{
}
itemView.setOnClickListener(view -> {
Toast.makeText(context, "Item clicked: " + trendingFilters.getId(), Toast.LENGTH_SHORT).show();
});
}
Вот метод для вычисления значение размера выборки, которое является степенью двойки на основе ширины и высоты цели:
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
Этот метод позволяет легко загрузить растровое изображение произвольно большого размера в ImageView, который отображает миниатюру размером 100x100 пикселей, как показано в следующем примере кода:
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
@OnClick(R.id.filters_image)
public void onViewClicked() {
}
}
И ошибка в logcat выглядит следующим образом:
W/Glide: Load failed for https://www.svrf.com/classic/embed/vr/1041219 with size [1141x284]
class com.bumptech.glide.load.engine.GlideException: Failed to load resource
Cause (1 of 3): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{ContentLengthInputStream->Object->Drawable}, REMOTE
Cause (1 of 3): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{ContentLengthInputStream->GifDrawable->Drawable}
Cause (2 of 3): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{ContentLengthInputStream->Bitmap->Drawable}
Cause (3 of 3): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{ContentLengthInputStream->BitmapDrawable->Drawable}