是否可以从文件系统(* .xml文件)创建VectorDrawable [英] Is it possible to create a VectorDrawable from File System (*.xml File)

查看:96
本文介绍了是否可以从文件系统(* .xml文件)创建VectorDrawable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Android应用中使用VectorDrawables.
我想从文件系统加载xml文件并获取 android.graphics.Drawable 的实例以在 ImageView 中显示它.如果我将xml文件添加到Resources目录中,它将起作用.但是,当我尝试从文件系统加载它时,我总是得到一个NullPointer.

I am trying to use VectorDrawables in my Android App.
I want to load an xml File from the File System and get an Instance of android.graphics.Drawable to display it in an ImageView. If i add the xml File to the Resources directory it works. But when i try to load it from the filesystem i always get a NullPointer.

我当前正在尝试通过加载文件 Drawable.createFromPath(*文件路径*) VectorDrawable.createFromPath(*文件路径*),但是我一直在获取NullPointer.该文件存在,并且是有效的xml文件(请参见下文).

I am currently trying to load the File via Drawable.createFromPath(*Path to File*) or VectorDrawable.createFromPath(*Path to File*), but i keep getting a NullPointer. The File exists and is a valid xml File (see below).

在adb日志中,我总是得到:

In adb log i always get :

SkImageDecoder::Factory returned null

当我使用 mContext.getFilesDir()时,路径看起来像

When i use mContext.getFilesDir() the Path looks something like

/data/data/*packagename*/files/*filename*.xml

我还尝试了一些公用文件夹,例如下载".当我使用File.io Api检查文件时,它 exists() canRead() canWrite()

I also tried some public Folders like "Downloads". When i check the File with the File.io Api it exists(), canRead() and canWrite()

更新,这是从android Dev Pages中提取的XML代码

Update This ist the XML Code taken from the android Dev Pages

 <vector xmlns:android="http://schemas.android.com/apk/res/android"
 android:height="64dp"
 android:width="64dp"
 android:viewportHeight="600"
 android:viewportWidth="600" >
 <group
     android:name="rotationGroup"
     android:pivotX="300.0"
     android:pivotY="300.0"
     android:rotation="45.0" >
     <path
         android:name="v"
         android:fillColor="#000000"
         android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
 </group>

推荐答案

不幸的是,没有.除非您以某种方式以编译资源的方式编译XML源文件,否则不会这样. VectorDrawable 当前采用已编译的图像源.

Unfortunately, no. Not unless you somehow compile the XML source file in the way that resources are compiled. VectorDrawable currently assumes a compiled image source.

否则,您可能已经编写了以下内容:

Otherwise you could probably have written something like this:

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,棉花糖6.0),因为膨胀调用尝试将属性集强制转换为 XmlBlock.Parser ,从而抛出 ClassCastException .原因记录在 aapt 工具打包的资源.

Currently that fails (API level 23, Marshmallow 6.0) because the inflate call attempts to cast the attribute set to an XmlBlock.Parser, which throws a ClassCastException. The cause is documented in the source; it will "only work with compiled XML files", such as resources packaged by the aapt tool.

这篇关于是否可以从文件系统(* .xml文件)创建VectorDrawable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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