How to show rtf content from raw file in TextView android?

怎甘沉沦 提交于 2020-08-17 12:18:52

问题


I want to load rtf file content from raw folder in textview. Can I convert rtf to html in android?. Below is my code,

RTF File Content

{\rtf1 \ansi \ansicpg1252 \deff0 \stshfdbch2 \stshfloch0 \stshfhich0 \deflang16393 \deflangfe16393 {\fonttbl {\f0 \froman \fcharset0 \fprq2 {*\panose 02020603050405020304}Times New Roman{*\falt Times New Roman};}{\f2 \fnil \fcharset134 \fprq0 {*\panose 02010600030101010101}SimSun{*\falt SimSun};}{\f3 \fnil \fcharset2 \fprq0 {*\panose 05000000000000000000}Wingdings{*\falt Wingdings};}{\f4 \froman \fcharset2 \fprq0 {*\panose 05050102010706020507}Symbol{*\falt Symbol};}{\f5 \fswiss \fcharset0 \fprq0 {*\panose 020b0604030504040204}Verdana{*\falt Verdana};}{\f6 \fswiss \fcharset0 \fprq0 {*\panose 020b0604020202020204}Arial{*\falt Arial};}}{\colortbl;\red0\green0\blue0;\red128\green0\blue0;\red255\green0\blue0;\red0\green128\blue0;\red128\green128\blue0;\red0\green255\blue0;\red255\green255\blue0;\red178\green187\blue29;\red35\green31\blue32;\red0\green0\blue128;\red128\green0\blue128;\red0\green128\blue128;\red128\green128\blue128;\red0\green85\blue150;\red192\green192\blue192;\red0\green0\blue255;\red255\green0\blue255;\red0\green255\blue255;\red255\green255\blue255;\red0\green0\blue0;\red255\green255\blue255;}

Code

 val raw: InputStream = resources.openRawResource(resourceID)
    val stream = ByteArrayOutputStream()
    var i: Int
    try {
        i = raw.read()
        while (i != -1) {
            stream.write(i)
            i = raw.read()
        }
        raw.close()
    } catch (e: IOException) {
        e.printStackTrace()
    }

    ConvertHTML=Html.fromHtml(stream.toString(),HtmlCompat.FROM_HTML_MODE_LEGACY)

    text_view.text = ConvertHTML;

来源:https://stackoverflow.com/questions/63249616/how-to-show-rtf-content-from-raw-file-in-textview-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!