I have a need to load 9-patch files from outside of the drawable folder. This is so that my application can download new skins from a server, for example. I have discovered that a 9-patch image stored in the drawable folder is compiled when the .apk is made. The exact same file read from the assets folder does not have the 9-patch chunk. Thus the act of making the .apk is compiling the source 9-patch files in the drawable folder, but not in the assets directory.
How can I compile a 9-patch file myself, so that I can install it in the assets directory? Is there a (batch) tool to convert the source to the compiled version with the 9-patch chunk? I’d really, really like not having to use Eclipse/Ant to build an .apk and then pick it apart to extract the compiled 9-patch file if at all possible.
For now I just want to be able to read from the assets directory (for example, have a sub directory per skin) to keep it simple. The next step is to compile the source image to add in the 9-patch chunk. After that I’ll worry about downloading on the fly to the /data folder – if I can’t compile the 9-patch file then there’s little point in adding server side effort.
There is no simple way to do this afaik. 9-patch compilation is done by aapt and is rather simple: it discards the black borders and encodes their content inside a PNG chunk. It would be fairly trivial for you to write a tool that does something similar. Note that you don’t even need to use the same format. If you look at the various NinePatch APIs in the doc, you will see that you can submit your own “chunk” (which encodes the stretch regions and padding.) The structure of a chunk byte[] array is explained here: