Load a vector drawable into imageview from sd card

让人想犯罪 __ 提交于 2019-11-30 14:17:54

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/38582698/load-a-vector-drawable-into-imageview-from-sd-card

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!