skip to Main Content

I have tried to import svg files in Android Studio via

New – Import Asset …

but it seems there is no fully support for svg … it converts the file to xml but the text disappears

e.g.

  ERROR @ line 27: <text> is not supported
ERROR @ line 27: <tspan> is not supported

Are there other ways to achieve the goal?

edit:
I have tried now …

    @OptIn(ExperimentalFoundationApi::class)
@Composable
fun Show_map(picName:String) {
    val mContext = LocalContext.current

    Canvas(modifier = Modifier.fillMaxSize()) {
        val svgString = loadSvgFromRaw(mContext, R.raw.nomap)
        val imageBitmap = convertSvgToImageBitmap(mContext,svgString!!)
        imageBitmap?.let {
            drawImage(
                image = it,
                topLeft = Offset.Zero,
                alpha = 1.0f
            )
        }
    }
}
fun loadSvgFromRaw(context: Context, resourceId: Int): String? {
    return try {
        val inputStream = context.resources.openRawResource(resourceId)
        val size = inputStream.available()
        val buffer = ByteArray(size)
        inputStream.read(buffer)
        inputStream.close()
        String(buffer, Charsets.UTF_8)
    } catch (e: Exception) {
        e.printStackTrace()
        null
    }
}

fun convertSvgToImageBitmap(context: Context,svgString: String): ImageBitmap {
    val svgString = loadSvgFromRaw(context, R.raw.nomap)
    val svg = SVG.getFromString(svgString)
    val targetWidth = 200 
    val targetHeight = 200 

    val requestOptions = RequestOptions()
        .override(targetWidth, targetHeight)
        .diskCacheStrategy(DiskCacheStrategy.NONE)
        .skipMemoryCache(true)

    val drawable = Glide.with(context)
        .`as`(Drawable::class.java)
        .load(svg)
        .transition(DrawableTransitionOptions.withCrossFade())
        .apply(requestOptions)
        .submit()
        .get()

    return (drawable as BitmapDrawable).bitmap.asImageBitmap()
}

But got an error at get() on runtime – You must call this method on a background thread – before shows Android Studio no error

Any ideas? – app shows a white Activity

2

Answers


  1. Chosen as BEST ANSWER
        @OptIn(ExperimentalFoundationApi::class)
    @Composable
    fun Show_map(picName:String) {
        val mContext = LocalContext.current
    
        Canvas(modifier = Modifier.fillMaxSize()) {
            val svgString = loadSvgFromRaw(mContext, R.raw.nomap)
    
            GlobalScope.launch {
                val imageBitmap = convertSvgToImageBitmap(mContext,svgString!!)
    
              //   SVG.registerExternalFileResolver()
    
                imageBitmap?.let {
                    drawImage(
                        image = it,
                        topLeft = Offset.Zero,
                        alpha = 1.0f
                    )
                }
            }
    
        }
    }
    fun loadSvgFromRaw(context: Context, resourceId: Int): String? {
        return try {
            val inputStream = context.resources.openRawResource(resourceId)
            val size = inputStream.available()
            val buffer = ByteArray(size)
            inputStream.read(buffer)
            inputStream.close()
            String(buffer, Charsets.UTF_8)
        } catch (e: Exception) {
            e.printStackTrace()
            null
        }
    }
    
    suspend fun convertSvgToImageBitmap(context: Context,svgString: String): ImageBitmap {
        val svgString = loadSvgFromRaw(context, R.raw.nomap)
        val svg = SVG.getFromString(svgString)
        val targetWidth = 200
        val targetHeight = 200
    
        val requestOptions = RequestOptions()
            .override(targetWidth, targetHeight)
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .skipMemoryCache(true)
    
        val drawable = Glide.with(context)
            .`as`(Drawable::class.java)
            .load(svg)
            .transition(DrawableTransitionOptions.withCrossFade())
            .apply(requestOptions)
            .submit()
            .get()
    
        return (drawable as BitmapDrawable).bitmap.asImageBitmap()
    }
    

    I have changed the code it does not work anyway get an error

    Failed to find any ModelLoaders registered for model class: class com.caverock.androidsvg.SVG


  2. Use AndroidSVG library: AndroidSVG is a library that allows you to directly render SVG images in Android. You can add the library to your project by including the following dependency in your app’s build.gradle file:

    implementation 'com.caverock:androidsvg:1.4'

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search