Network SVG to Bitmap
I want to display an image in my notification. I'm using the following code to get the image from the server,
public Bitmap getBitmapFromURL(String strURL) {
try {
URL url = new URL(strURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
It works fine for normal image formats. But sometimes the server will return SVG images. The above code does not work for SVG. How can I make it work in both SVG and jpeg formats? Any help would be appreciated.
Solution 1:
Android does not support downloading and viewing SVG out of the box only thing you can do is add them as vector drawables in your build but you cannot download and show svg images on the fly. Adding vector assets to your build is explained here in detail.
There are some libraries like this that provide support for SVG but the support is quite weak and would not be able support all the features of SVG.
I would recommend the data be filtered at your backend and instead of using SVG use jpg or webp formats that are supported out of box on android platform.