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.
Please refer to Having issue on Real Device using vector image in android. SVG-android
In the users post he asks a similar question and suggest he uses:
Create a member variable for the ImageView in your layout file;
private ImageView mImageView;
// intialize in onCreate(Bundle savedInstanceState)
mImageView = (ImageView) findViewById(R.id.image_view);
Download the image
private class HttpImageRequestTask extends AsyncTask<Void, Void, Drawable> {
@Override
protected Drawable doInBackground(Void... params) {
try {
final URL url = new URL("http://upload.wikimedia.org/wikipedia/commons/e/e8/Svg_example3.svg");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
SVG svg = SVGParser. getSVGFromInputStream(inputStream);
Drawable drawable = svg.createPictureDrawable();
return drawable;
} catch (Exception e) {
Log.e("MainActivity", e.getMessage(), e);
}
return null;
}
@Override
protected void onPostExecute(Drawable drawable) {
// Update the view
updateImageView(drawable);
}
}
Then Apply the drawable to the Imageview
@SuppressLint("NewApi")
private void updateImageView(Drawable drawable){
if(drawable != null){
// Try using your library and adding this layer type before switching your SVG parsing
mImageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
mImageView.setImageDrawable(drawable);
}
}
SVGParser is available at https://github.com/pents90/svg-android