2

I want to display a .svg file in an imageview or a webview. I don't know which one is better for this purpose, would like some opinions on this subject.

Keep in mind that I don't have the file on compile time so I can't use the VectorDrawable API 21 method with Android Studio, although I can use the class if it helps.

First I need to search if there is no .svg stored in the filesystem (private app storage), if not found I have to download and save it on the filesytem. I will use OkHTTP to download from the web and save the file.

I already have glide on this project if this help on displaying a .svg I would like suggestions since I never worked with .svg using glide.

For this kind of problem should I use old libraries like svg-android? I don't know if they are reliable anymore.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Exprove
  • 1,301
  • 2
  • 18
  • 32

1 Answers1

2

You can use Glide for svg images. Create a requestBuilder :

GenericRequestBuilder<Uri, InputStream, SVG, PictureDrawable> requestBuilder = Glide.with(this)
                .using(Glide.buildStreamModelLoader(Uri.class, this), InputStream.class)
                .from(Uri.class)
                .as(SVG.class)
                .transcode(new SvgDrawableTranscoder(), PictureDrawable.class)
                .sourceEncoder(new StreamEncoder())
                .cacheDecoder(new FileToStreamDecoder<>(new SvgDecoder()))
                .decoder(new SvgDecoder())
                .listener(new SvgSoftwareLayerSetter<Uri>());

And then : requestBuilder.diskCacheStrategy(DiskCacheStrategy.NONE).load(Uri.parse(svgUrl)).into(imageView);

See this example.

Create svgUrl like this :

    String fileName = "name.jpg";
    String path = Environment.getExternalStorageDirectory() + "/" + fileName;

    File file = new File(path);
    Uri svgUrl = Uri.fromFile(file);
Newaj
  • 3,992
  • 4
  • 32
  • 50
  • Thanks, can I change the tint and background color of the .svg? I have glide 4.0.0 in my project, do you have any idea how can I migrate that code to the new version? – Exprove Aug 22 '17 at 15:30
  • For tint, try this in `ImageView` : `android:tint="@color/yourColor"` – Newaj Aug 22 '17 at 15:32
  • Isn't it the latest version? – Newaj Aug 22 '17 at 15:35
  • Yes it is. That code is for 3.6 I guess. Trying to follow this: http://bumptech.github.io/glide/doc/migrating.html , but without success – Exprove Aug 22 '17 at 15:36
  • No, there is no such thing as GenericRequestBuilder, and these methods don't exist: using(), from(), etc – Exprove Aug 22 '17 at 15:45
  • I searched for that & found nothing impressive. If you have no other issue, you may prefer using glide 3.6.0. Also there is svg-android library for svg image. https://code.google.com/archive/p/svg-android/ – Newaj Aug 22 '17 at 15:58