我想在我的Android应用程序中使用VectorDrawables.
我想从文件系统加载一个xml文件,并获得一个android.graphics.Drawable实例,以在ImageView中显示它.如果我将xml文件添加到Resources目录,它可以工作.但是当我尝试从文件系统加载它时,我总是得到一个NullPointer.
我目前正在尝试通过加载文件
Drawable.createFromPath(* Path to File *)或VectorDrawable.createFromPath(* Path to File *),但我不断获得NullPointer.
文件存在且是一个有效的xml文件(见下文).
在adb日志中我总是得到:
SkImageDecoder::Factory returned null
当我使用mContext.getFilesDir()时,Path看起来像
/data/data/*packagename*/files/*filename*.xml
我还尝试了一些像“下载”这样的公共文件夹.当我使用File.io Api检查文件时,它存在(),canRead()和canWrite()
更新这是从Android Dev Pages获取的XML代码
android:height="64dp"
android:width="64dp"
android:viewportHeight="600"
android:viewportWidth="600" >
android:name="rotationGroup"
android:pivotX="300.0"
android:pivotY="300.0"
android:rotation="45.0" >
android:name="v"
android:fillColor="#000000"
android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
解决方法:
很不幸的是,不行.除非您以编译资源的方式以某种方式编译XML源文件. VectorDrawable当前假定已编译的图像源.
否则你可能会写这样的东西:
VectorDrawable d = new VectorDrawable();
try( InputStream in = new FileInputStream( *Path to File* ); )
{
XmlPullParser p = Xml.newPullParser();
p.setInput( in, /*encoding, self detect*/null );
d.inflate( getResources(), p, Xml.asAttributeSet(p) ); // FAILS
}
目前失败(API级别23,Marshmallow 6.0),因为inflate调用尝试将属性集强制转换为XmlBlock.Parser,这会抛出ClassCastException.原因记录在source;它将“仅适用于已编译的XML文件”,例如aapt工具打包的资源.
标签:android,xml,vectordrawable
来源: https://codeday.me/bug/20190516/1116908.html