Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load a vector drawable into imageview from sd card

I want to show a vector image (say vectorimage.xml) in an imageview from the sd card. Please throw some insight on how to do this in android. What I have tried already :-

String imagePath = Environment.getExternalStorageDirectory() + "/ folder/productImage.xml";

bitmapImage = BitmapFactory.decodeFile(imagePath);
Drawable bgrImageDrawable = new BitmapDrawable(bitmapImage);

The above code snippet does not work since the bitmapImage is coming as null.

like image 824
Ramya BA Avatar asked Jul 26 '16 06:07

Ramya BA


People also ask

What is not an advantage of using vector Drawables?

The drawback is that they might take longer to draw, etc since there's more of parsing and drawing going on than just using a bitmap. This should although be neglectable if you keep your vectors simple.

What is SRC in ImageView?

android:scaleType. Controls how the image should be resized or moved to match the size of this ImageView. android:src. Sets a drawable as the content of this ImageView.

How do I use SVG files on android?

If the image is available in your computer then select the local svg file. After that, select the image path. An option to change the size of the image is also available at the right side of dialog if you want to. In this way, the SVG image is imported in your project.


1 Answers

The BitmapFactory can't load vector drawables. You have to use the VectorDrawable or VectorDrawableCompat class. To load a vector drawable you need to use a xml loader.

Some parser like the one for the resources need a precompiled binary xml file. You can find them in the apk file when you put the vector drawable in the drawable resource directory.

Here is a sample to load it from the assets, you should be able to use a similar code for the loading from the sd card.

final XmlResourceParser parser = context.getAssets().openXmlResourceParser("assets/folder/image.xml");
drawable = VectorDrawableCompat.createFromXml(context.getResources(), parser);

This way needs at least Android 5.0

like image 186
Rocket Avatar answered Oct 18 '22 03:10

Rocket