Search code examples
javaandroidandroid-layoutsvgandroid-glide

How to load svg image to image view using Glide


I have a SVG image in this URL. svg image link . I want to load this SVG image from url into image view. I have used Glide to load this SVG image to image view.

Here's the Glide Code:

 Glide.with( context)
                .setDefaultRequestOptions(new RequestOptions().timeout(60000))
                .load( "https://restcountries.eu/data/zwe.svg" )
                .thumbnail( 0.5f )
                .override( 200, 200 )
                .diskCacheStrategy( DiskCacheStrategy.ALL )
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        progressLayout.setVisibility(View.GONE);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        progressLayout.setVisibility(View.GONE);
                        flag_iv.setEnabled(true);
                        return false;
                    }

                })
                .into( flag_iv );

XML Code:

<ImageView
     android:src="@drawable/icon_app"
     android:id="@+id/flag_iv"
     android:layout_width="match_parent"
     android:layout_height="50dp"
     android:layout_marginTop="8dp"
     android:layout_marginBottom="8dp"
 />

But it doesn't get loaded. It shows the below error.

W/Glide: Load failed for https://restcountries.eu/data/afg.svg with size [100x100]

I have used png/jpg images using Glide. It worked Great. Why SVG images are not loading using Glide. I searched Stack Overflow. Everyone suggesting different Libraries. But they are outdated. I want to achieve it using Glide. Please help me with some solutions.


Solution

  • You can use the Glide To Vector,

    repo : https://github.com/corouteam/GlideToVectorYou

    and to load svg into imageview you can use:

    GlideToVectorYou
                    .init()
                    .with(this)
                    .withListener(new GlideToVectorYouListener() {
                        @Override
                        public void onLoadFailed() {
                            Toast.makeText(context, "Load failed", Toast.LENGTH_SHORT).show()
                        }
    
                        @Override
                        public void onResourceReady() {
                            Toast.makeText(context, "Image ready", Toast.LENGTH_SHORT).show()
                        }
                    })
                    .setPlaceHolder(placeholderLoading, placeholderError)
                    .load(IMAGE_URL, imageview);