Glide: .error() resource not used when loading null

Created on 21 Mar 2016  路  7Comments  路  Source: bumptech/glide

Glide Version: 3.7.0
Integration libraries: no
Device/Android Version: GT-I9500 5.0.1
Issue details / Repro steps / Use case background:

  1. if pathToImage = null for load function - I get exception callback (this is right) with Exception object is null (this is incorrect)
  2. If pathToImage = null why not loading 'error' resource which was set on start loading image ?
  3. Perhaps, if error would be call right (my point 2), result would not be transformer by setting callback function.
  4. About placeholder. If I need set placeholder as resource drawable (not as color in my code below), then It would not be transformer by callback function. For example, I can do shape with dublicate round fill color. What about if I need user image for placeholder ? I need transfrom not only correct result, but all results and placeholder too.

Glide load line / GlideModule (if any) / list Adapter code (if any):
in class which is extends from ImageView

Glide.with(getContext())
                .load(pathToImage)
                .bitmapTransform(new RoundedCornersTransformation(getContext(), 100, 0))
                .placeholder(R.color.orange_red)
                .error(defaultPhotoId)
                .dontAnimate()
                .listener(this)
                .into(this);

Layout XML:

<com.application.ImageGlide
        android:id="@+id/avatar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

I user only square image

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int width = MeasureSpec.getSize(widthMeasureSpec);
        if (width > 0) {
            setMeasuredDimension(width, width);
        }
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }

1.png - start screen
2.png - scroll bottom and return top
1
2

question

Most helpful comment

Yes, you need to do some work on the placeholder, because there's no general way to do transforms.
You have two options:

java private BitmapDrawable transformDrawable(Drawable drawable, Transformation<Bitmap> transform, int size) { Bitmap bitmap = Bitmap.createBitmap(size, size, Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, size, size); drawable.draw(canvas); Resource<Bitmap> original = BitmapResource.obtain(bitmap, Glide.get(getContext()).getBitmapPool()); Resource<Bitmap> rounded = transform.transform(original, size, size); if (!original.equals(rounded)) { original.recycle(); } return new BitmapDrawable(getResources(), rounded.get()); }

All 7 comments

  • Placeholder transformation: https://github.com/bumptech/glide/issues/317#issuecomment-96429525
    It's also worth noting that Drawables are not Bitmaps, so you cannot transform them. Think about a VectorDrawable or PictureDrawable, how do you expect that to be blurred or round-cropped for example? For placeholder-like drawables you'll have to define a shape in XML similar to your transformation.
  • Exception null: there's no error here, that's intentional. There was no exception, you passed a null, you get a null, it was handled. NPE is logically reserved to unintentional null accesses, which didn't happen inside Glide for this case.
  • load(null): https://github.com/bumptech/glide/issues/268 so you just need to do .error(R.drawable.x).fallback(R.drawable.x) if you want to treat null as error.

Btw, why did you close the issue? Accident or found a solution?

@TWiStErRob error resource would be use if for 'onException' callback return false.
But now I can see what error resource is use transform before display.

As placeholder I use shape (it's not problem). But if I use non solid drawable (vector for example) - it is'not use transformation. And this is really bad, because need preparatory work for placeholder.

Yes, you need to do some work on the placeholder, because there's no general way to do transforms.
You have two options:

java private BitmapDrawable transformDrawable(Drawable drawable, Transformation<Bitmap> transform, int size) { Bitmap bitmap = Bitmap.createBitmap(size, size, Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, size, size); drawable.draw(canvas); Resource<Bitmap> original = BitmapResource.obtain(bitmap, Glide.get(getContext()).getBitmapPool()); Resource<Bitmap> rounded = transform.transform(original, size, size); if (!original.equals(rounded)) { original.recycle(); } return new BitmapDrawable(getResources(), rounded.get()); }

Ty

it's not work for MultiTransformation?
I'm using CenterCrop and RoundedCorners to load image. and use the transformDrawable() methd to load placeholder.but it's not working. this is my code:

  MultiTransformation transformation = new MultiTransformation(new CenterCrop(), new RoundedCorners(roundingRadius));
        BitmapDrawable bitmapDrawable = transformDrawable(context, placeholder,
                transformation);
        GlideApp.with(context)
                .load(url)
                .placeholder(bitmapDrawable)
                .error(bitmapDrawable)
                .transform(transformation)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);

@TWiStErRob when you say "Once!" does it mean we have to keep global reference for that BitmapDrawable, say when we load the app and later we can use same reference everywhere throughout the app? Please correct me if I am wrong. Thanks.

Was this page helpful?
0 / 5 - 0 ratings