将可绘制的矢量从SD卡加载到imageview中 [英] Load a vector drawable into imageview from sd card

查看:90
本文介绍了将可绘制的矢量从SD卡加载到imageview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在SD卡的imageview中显示矢量图像(例如vectorimage.xml).请对如何在android中执行此操作提出一些见解. 我已经尝试过的:-

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

由于bitmapImage为null,因此上述代码段不起作用.

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

推荐答案

BitmapFactory无法加载矢量可绘制对象. 您必须使用VectorDrawable或VectorDrawableCompat类. 要加载矢量可绘制对象,您需要使用xml加载器.

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.

像这样的用于资源的解析器需要一个预编译的二进制xml文件.将矢量drawable放置在drawable资源目录中时,可以在apk文件中找到它们.

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.

这里是一个从资产加载示例,您应该能够使用类似的代码从sd卡加载.

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

这种方式至少需要Android 5.0

This way needs at least Android 5.0

这篇关于将可绘制的矢量从SD卡加载到imageview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆