Android:Load Svg File from Web and Show It on Image View

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

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

How to load svg URL with glide and set in ImageView?

It's not that easy with Glide to do from scratch, I would recommend using this library

I have used this in one of my project, under the hood it used Glide and gets the job done.

public class MainActivity extends AppCompatActivity {

private ImageView image;
private String url;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

url = "http://www.clker.com/cliparts/u/Z/2/b/a/6/android-toy-h.svg";

image = (ImageView) findViewById(R.id.image);

SvgLoader.pluck()
.with(this)
.setPlaceHolder(R.mipmap.ic_launcher, R.mipmap.ic_launcher)
.load(url, image);
}

@Override protected void onDestroy() {
super.onDestroy();
SvgLoader.pluck().close();
}
}

Load svg file from url on ImageView

ImageView only accepts Bitmaps or VectorDrawables.

An SVG is neither of the two, even if VectorDrawable descends from it.

If you don't want to use an external library to transform the SVG into a Bitmap, then I can suggest you to use a WebView to display it


Alternatively, you might want to convert the SVG to a VectorDrawable by using a free online tool.

Download and display svg image in Android

use this library
AndroidSvgLoader

How to load svg image to image view using Glide

You can use the Glide To Vector,

repo : https://github.com/corouteam/GlideToVectorYou

and to load svg into imageview you can use:

GlideToVectorYou
.init()
.with(this)
.withListener(new GlideToVectorYouListener() {
@Override
public void onLoadFailed() {
Toast.makeText(context, "Load failed", Toast.LENGTH_SHORT).show()
}

@Override
public void onResourceReady() {
Toast.makeText(context, "Image ready", Toast.LENGTH_SHORT).show()
}
})
.setPlaceHolder(placeholderLoading, placeholderError)
.load(IMAGE_URL, imageview);


Related Topics



Leave a reply



Submit