Skip to content

.error() resource not used when loading null #1077

@gromiloff

Description

@gromiloff

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

Activity

TWiStErRob

TWiStErRob commented on Mar 21, 2016

@TWiStErRob
Collaborator
  • Placeholder transformation: Is possible to make transformation to placeholder as well? #317 (comment)
    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): .load(null) can be a valid .load request #268 so you just need to do .error(R.drawable.x).fallback(R.drawable.x) if you want to treat null as error.
TWiStErRob

TWiStErRob commented on Mar 21, 2016

@TWiStErRob
Collaborator

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

gromiloff

gromiloff commented on Mar 21, 2016

@gromiloff
Author

@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.

added a commit that references this issue on Mar 21, 2016
TWiStErRob

TWiStErRob commented on Mar 21, 2016

@TWiStErRob
Collaborator

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

  • play around with XML enough to be able to round-crop any drawable, shape, mask, etc.

  • programmatically render and crop it (once!) before using as a placeholder in the list.
    For full example see TWiStErRob/glide-support@1b4d9d3

    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());
    }
changed the title [-]Error resource using issue[/-] [+].error() resource not used when loading null[/+] on Mar 21, 2016
gromiloff

gromiloff commented on Mar 21, 2016

@gromiloff
Author

Ty

MichaelJokAr

MichaelJokAr commented on Dec 8, 2017

@MichaelJokAr

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);
chitrang200889

chitrang200889 commented on Sep 11, 2020

@chitrang200889

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @gromiloff@TWiStErRob@chitrang200889@MichaelJokAr

        Issue actions

          .error() resource not used when loading null · Issue #1077 · bumptech/glide