Glide 4.10.0 SVG example code not working for me

145 views
Skip to first unread message

Sarah Maslin Abram

unread,
Nov 2, 2022, 5:49:53 AM11/2/22
to Glide
Hi all

I am very new to Glide so I apologise if I'm asking something obvious.

I have been tasked with adding support for rendering SVGs in an ImageView using Glide.  We already have the code in our application which is loading non-svg files into the image view.

We are using Glide 4.10.0 and I have added the AndroidSVG library already (as i am using it elsewhere).

I have taken the code exactly from the 4.10.0 sample SVG project.  

First I copied the SvgDecoder.java / SvgDrawableTranscoder.java / SvgModule.java exactly as is and put them in their own package.

In my code where I am loading images into an ImageView, I added code to support SVGs.

    protected void loadAssetThumbnail(GlideUrl thumbnailUrl, boolean isSVG) {

        if (isSVG) {
            Glide.with(thumbnailImageView.getContext())
                .as(PictureDrawable.class)
                .load(glideUrl)

                .placeholder(R.color.file_placeholder)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .listener(new RequestListener<PictureDrawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model,
                                                Target<PictureDrawable> target, boolean isFirstResource) {
                        DataWorkflowTasks.log("thumbnail load failed!" + e.getMessage());

                        ImageView view = ((ImageViewTarget<?>) target).getView();
                        view.setLayerType(ImageView.LAYER_TYPE_NONE, null);

                        return false;
                    }

                    @Override
                    public boolean onResourceReady(PictureDrawable resource, Object model,
                                                   Target<PictureDrawable> target, DataSource dataSource,
                                                   boolean isFirstResource) {
                        ImageView view = ((ImageViewTarget<?>) target).getView();
                        view.setLayerType(ImageView.LAYER_TYPE_SOFTWARE, null);

                        //on load success
                        return false;
                    }
                })
                .into(thumbnailImageView);
        } else {

            // load preview image using glide
            Glide.with(thumbnailImageView.getContext())
                .load(glideUrl)

                .placeholder(R.color.file_placeholder)
                .diskCacheStrategy(DiskCacheStrategy.NONE)  
                .skipMemoryCache(true)  
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model,
                                                Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model,
                                                   Target<Drawable> target, DataSource dataSource,
                                                   boolean isFirstResource) {
                        return false;
                    }
                })
                .into(thumbnailImageView);
        }
    }



When i run this code, it does not load my SVG, the code comes into the onLoadFailed in my listener. The exception just shows that the load failed, but does not give me any clue as to why its failing.

I presume I have missed something very simple to get it to use the additional glide files to convert the SVG but I am not sure what I have missing.

Any help would be greatly appreciated.

Sarah


Sarah Maslin Abram

unread,
Nov 3, 2022, 11:19:39 AM11/3/22
to Glide
As expected I had not set up Glide correctly. I was missing this line in my build.gradle file:

    annotationProcessor 'com.github.bumptech.glide:compiler:4.14.2'

Furthermore my Glide command should have been using "GlideApp" not "Glide", this is so that is uses the  extra Glide files (SvgDecoder/SvgModule/SvgDrawableTranscoder) handling the SVG file.

I also had to comment out the placeholder image otherwise i was getting an error about bitmap missing the src attribute.

   Glide.with(thumbnailImageView.getContext())
    .as(PictureDrawable.class)
    .load(glideUrl)


    .listener(new RequestListener<PictureDrawable>() {
        @Override
        public boolean onLoadFailed(@Nullable GlideException e, Object model,
                                    Target<PictureDrawable> target, boolean isFirstResource) {
            DataWorkflowTasks.log("thumbnail load failed!" + e.getMessage());

            ImageView view = ((ImageViewTarget<?>) target).getView();
            view.setLayerType(ImageView.LAYER_TYPE_NONE, null);
           
            return false;
        }

        @Override
        public boolean onResourceReady(PictureDrawable resource, Object model,
                                       Target<PictureDrawable> target, DataSource dataSource,
                                       boolean isFirstResource) {
            ImageView view = ((ImageViewTarget<?>) target).getView();
            view.setLayerType(ImageView.LAYER_TYPE_SOFTWARE, null);

            //on load success
            return false;
        }
    })
    .into(thumbnailImageView);
Reply all
Reply to author
Forward
0 new messages