Android Fresco Load Vector Drawable From Uri

November 22, 2019
val iconUri = UriUtil.getUriForResourceId(R.drawable.ic_add_black_24dp)

val vectorResourceId = if (iconUri != null && UriUtil.isLocalResourceUri(iconUri)) {
    val resourceId = iconUri.pathSegments[0].toInt()
    val fileExt = context.resources.getFileExt(resourceId)
    if (fileExt == "xml") // probably a vector
        resourceId
    else
        null
} else null

if (vectorResourceId != null) {
    simpleDraweeView.hierarchy.setPlaceholderImage(vectorResourceId)
    // optional
    // simpleDraweeView.setImageURI(null as Uri?)

    // deprecated
    // simpleDraweeView.setImageResource(vectorResourceId)

    // use imageView.setImageResource(vectorResourceId)

    // this would not work for vector resource
    // simpleDraweeView.setActualImageResource(vectorResourceId)
} else {
    simpleDraweeView.setImageURI(item.iconUri)
}

NOTE: Technically, Freso doesn’t support vector image.

NOTE: Android Check Resources Id Is Vector

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.