Inflate XML file from internal memory
Asked Answered
A

2

3

I've copied an XML file from /assets to my applications data folder (data/data/package.name/files/). I'm doing this because the user will be able to modify a lot of data, and I want to save that data to the internal memory and then load it again when they restart the app. This all works well, using Root Browser I can see the XML file is properly copied to the data directory.

Now I need to inflate this XML file using a LayoutInflater. How would I access this file? With an XmlResourceParser or XmlPullParser?

Analog answered 19/11, 2011 at 22:24 Comment(6)
check this tutorial - developer.android.com/intl/zh-CN/guide/topics/data/…Dunfermline
The question was not about writing to internal storage..Analog
The tutorial has a reference to openFileInput(). Sorry - I should have pointed to that more explicitly. This way you can "access that file".Dunfermline
Well I guess you have a point.. maybe I wasn't specific enough. I know how to "access" it, but I need to inflate it.Analog
Sorry for my poor English. I think we can do by creating views in dynamically using tags of the XML. But I do not know it good for android system.Shank
Sorry for my poor English. I think we can do by creating views in dynamically using tags of the XML. But I do not know it good for android system.Shank
B
2

You cannot inflate that file using LayoutInflater. First, you can only inflate layout resources, not arbitrary files. Second, based on the description in your first paragraph, it is not a layout file in the first place.

If you want to parse arbitrary XML, use the DOM, SAX, or XmlPullParser.

Blakeley answered 19/11, 2011 at 22:45 Comment(3)
Well, it is a standard Android XML file. When the user pushes the save button I'll parse their input into an XML file. I'll look at those classes and post back.. thanks!Analog
Ok, I've got it working (I was playing Skyrim for a bit :P ). I ditched storing the file as a typical Android XML layout and I'm now loading it with XmlPullParser. I guess "No, you can't" suffices as an answer ;)Analog
@Snailer: Yeah, it'd be nice to inflate layout files from arbitrary sources. With some work, you might even be able to hack in a solution for that. But it's definitely not supported out of the box. Also, my apologies for assuming that your original file wasn't actually a layout -- it felt more like just ordinary XML-encoded data from what you wrote.Blakeley
S
1

From the Android view API:

it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime.

A solution might be to pre-process XML files before. You must compress the XML file in the same way the Android SDK does at build time:

view inflation relies heavily on pre-processing of XML files that is done at build time

I guess you will have to call XmlPullParser.setInput to load your pre-processed XML, and then pass the XmlPullParser to View.inflate. I hope it is possible to do this kind of stuff even for content that is not in the APK.

I am very interested in any solution you can find. Please let me know if you find or create a prototype! This would be a decisive step towards creating a plugin architecture for my Open Source app.

Susiesuslik answered 20/4, 2012 at 2:18 Comment(1)
I am also wondering for the same thing. I want to provide multiple themes for my android application. Main app can use layouts loaded in plug in app. So you got any solution ? Or any suggestion, demo, prototype if you found or still looking for the solution ?Kary

© 2022 - 2024 — McMap. All rights reserved.