Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android:load svg file from web and show it on image view

I want to load a svg file from the web and show this file in an ImageView. For non vector images I use the Picasso library.

Is it possible to use this library for svg files as well?
Is there any way to load svg files from the web and show it in an ImageView?
I use the svg-android library to show svg files but i don't know how to get svg images from the web all the examples for this library use local files.

like image 953
mahdi Avatar asked Jan 29 '15 13:01

mahdi


People also ask

How do I visualize an SVG file?

How Do I View an SVG File? All modern web browsers support viewing SVG files. That includes Chrome, Edge, Firefox, and Safari. So if you have an SVG and can't open it with anything else, open your favorite browser, select File > Open, then choose the SVG file you'd like to see.

How do I import an SVG file into an image?

SVG images can be written directly into the HTML document using the <svg> </svg> tag. To do this, open the SVG image in VS code or your preferred IDE, copy the code, and paste it inside the <body> element in your HTML document.

Can browser display SVG?

Browser SupportInternet Explorer 9 and later can display SVG natively. Firefox, Chrome, Safari, Opera and the Android browser have been able to show SVG natively for a while, at the time of writing. That is also true for Safari for iOS, Opera's mini and mobile browsers, and Chrome for Android.

How to load SVG from URL in Android app?

Now we will look at How we can load SVG from its URL in our Android App. For creating a new Android Studio project just click on File > New > New Project. Make sure to choose your language as JAVA.

How to load SVG image in imageview using Java?

In this JAVA class, we are loading data from the URL in the form of the byte stream. The sharp library will convert this byte stream and will load the SVG image in our target ImageView. To create a new JAVA class navigate to the app > java > your apps package name >> Right-click on it and then click on it and click New > Java class.

Does Android imageview support SVGS?

By default, Android’s ImageView does not support SVGs (why?). After googling for a while I found a complicated solution for the above problem using Glide with a custom module in combination with AndroidSVG.

How to enable glide to load SVG files from web?

To enable Glide to load .svg from web, you have to add following three classes to project: class1, class2, class3 . And magic is here:


1 Answers

Update: For newer version please checkout the Glide Samples (https://github.com/bumptech/glide/tree/master/samples/svg)

-

You can use Glide (https://github.com/bumptech/glide/tree/v3.6.0) and AndroidSVG (https://bitbucket.org/paullebeau/androidsvg).

There is also a sample from Glide: https://github.com/bumptech/glide/tree/v3.6.0/samples/svg/src/main/java/com/bumptech/svgsample/app

Setup GenericRequestBuilder

requestBuilder = Glide.with(mActivity)     .using(Glide.buildStreamModelLoader(Uri.class, mActivity), InputStream.class)             .from(Uri.class)             .as(SVG.class)             .transcode(new SvgDrawableTranscoder(), PictureDrawable.class)             .sourceEncoder(new StreamEncoder())             .cacheDecoder(new FileToStreamDecoder<SVG>(new SvgDecoder()))             .decoder(new SvgDecoder())                     .placeholder(R.drawable.ic_facebook)                     .error(R.drawable.ic_web)             .animate(android.R.anim.fade_in)             .listener(new SvgSoftwareLayerSetter<Uri>()); 

Use RequestBuilder with uri

Uri uri = Uri.parse("https://de.wikipedia.org/wiki/Scalable_Vector_Graphics#/media/File:SVG_logo.svg");             requestBuilder                     .diskCacheStrategy(DiskCacheStrategy.SOURCE)                             // SVG cannot be serialized so it's not worth to cache it                     .load(uri)                     .into(mImageView); 
like image 88
Rags93 Avatar answered Sep 25 '22 19:09

Rags93