Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
gromiloff opened this issue Mar 21, 2016 · 7 comments
Closed

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

gromiloff opened this issue Mar 21, 2016 · 7 comments
Labels

Comments

@gromiloff
Copy link

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

@TWiStErRob
Copy link
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
Copy link
Collaborator

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

@gromiloff
Copy link
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.

TWiStErRob added a commit to TWiStErRob/glide-support that referenced this issue Mar 21, 2016
@TWiStErRob
Copy link
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());
    }

@TWiStErRob TWiStErRob changed the title Error resource using issue .error() resource not used when loading null Mar 21, 2016
@gromiloff
Copy link
Author

Ty

@MichaelJokAr
Copy link

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
Copy link

@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
Labels
Projects
None yet
Development

No branches or pull requests

4 participants