0

I have a String with xml's drawable code. I need to use this String inside ImageView like image. And I need to create many such images in activity using kotlin/java code.

I tried to write data to xml file and then convert file to Drawable

     /**
     * Write text to file 
     */
    fun stringToDom(xmlSource : String, filePath: String) {
        val fw = FileWriter(filePath)
        fw.write(xmlSource)
        fw.close()
    }

    //...
    val uri : Uri = Uri.parse("android.resource://com.example.package/" + R.drawable.temp)
    stringToDom(i, uri.toString())
    val d : Drawable = Drawable.createFromXml(context.resources, context.resources.getXml(R.drawable.temp))
    //...

And parse xml string with XmlPullParser

     val parser : XmlPullParser = Xml.newPullParser();
     parser.setInput(StringReader(myXmlString));
     val d : Drawable = Drawable.createFromXml(resources, parser)

But all of this doesn't work.

  • 1
    "I need to use this String inside ImageView like image" -- either use a `TextView`, or draw text into your `ImageView` (or just a plain `View`) using a `Canvas`. – CommonsWare Feb 05 '22 at 19:13
  • @CommonsWare This String contains vector drawable code. I need to show this vector image(or raster). I don't need to show code of image like text – Вікторія .Палихата Feb 05 '22 at 22:50
  • If the string is an SVG, or could be converted into an SVG, there are SVG rendering libraries available. Or, perhaps [try `VectorDrawableCompat`](https://developer.android.com/reference/kotlin/androidx/vectordrawable/graphics/drawable/VectorDrawableCompat#createfromxmlinner). – CommonsWare Feb 05 '22 at 22:53
  • @CommonsWare VectorDrawableCompat as a Drawable need this string parsed with XmlPullParser but I get an error when i'm trying to parse. – Вікторія .Палихата Feb 10 '22 at 09:50
  • @Вікторія.Палихата Is the drawable string what you would see in a text editor? In other words, are you trying to compile and display a drawable from a string as you would see it in the "/res/drawable" folder in Android Studio? What is the source of the string? Is it coming over a network? – Cheticamp Feb 10 '22 at 12:47
  • @Cheticamp This string is coming over a network. I get it from xml file in cloud storage. – Вікторія .Палихата Feb 10 '22 at 14:14

1 Answers1

0

The Android framework works with binary (compiled) XML files for drawables. If you are trying to work with with drawable XML source directly, you have some work to do. First you will have to compile the source XML into the binary format. This answer from Nicolas explains how to do that. It doesn't appear to be a complete answer, though, since it looks like it just compiles "Path" statements, but it's a start. This type of compilation is what the AAPT tool does for you through the regular tool chain. Other than AAPT, I don't know of another library that will do the compilation, but one may exist. I haven't tried Nicolas's code, but it looks right.

Once you have the compilation done, this answer, also from Nicolas, explains how to get the binary XML you create into a format that Android can use.

If your source refers to just Android resources (resource id = 0x01xxxxxx) then you will be able to match them within your app since Android resource ids are stable across releases. If your source refers to application resources (resource id = 0x7fxxxxxx), you will have trouble if the resource id doesn't exist in your app or is otherwise different.

(If this sounds like a little too much, you should consider an SVG converting library as CommonsWare suggests.)

If you can influence what comes over the network, you can precompile the drawables in the binary format within an app, extract the compiled XML and send that over the network. This will save you the first step since AAPT will do the compilation for you and leave you with just the second step. You will still need to ensure that there is no resource mismatch.

Cheticamp
  • 61,413
  • 10
  • 78
  • 131
  • Maybe it would be correctly if I will use svg instead xml (this files was converted from svg to xml) and convert it into Bitmap (or Drawable)? – Вікторія .Палихата Feb 10 '22 at 18:01
  • @Вікторія.Палихата I think that may be easiest if you can locate a library to turn the SVG into a vector drawable, although I do like the idea of downloading the binary XML instead of the source as I describe. I think that would work, but I have never done it and you may have other requirements. – Cheticamp Feb 10 '22 at 18:35
  • when I decide use xml, I thought that android can work with it easily than convert svg with library (cause xml is native for android). But now I see that it is harder. Thank you. I'll try use svg. – Вікторія .Палихата Feb 10 '22 at 20:33