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.
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);
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