Android textView 显示: STRING_TOO_LARGE

在Android中,字符串资源的长度限制是32KB,getString()方法返回的字符串资源的大小超过这个限制,就会抛出STRING_TOO_LARGE 这个错误。

我本地的临界值是:32.3 KB (33,090 字节)

小于等于33090时,能正常显示;大于33090时,显示:STRING_TOO_LARGE。

<string content=" ...."/>

问题点是:getResource().getString(R.string.content) 得到的是:STRING_TOO_LARGE。

如果你需要显示更大的字符串,你可以尝试以下几种解决方案:

1、分割字符串:你可以尝试将大字符串分割成几个小字符串,然后分别在TextView中显示。这可以通过在字符串的特定位置使用分隔符来实现。
例如:

String largeText = "This is a very large string...";  
String[] smallerTexts = largeText.split("...");  
for (String text : smallerTexts) {  
    TextView textView = new TextView(this);  
    textView.setText(text);  
}

2、使用WebView:

如果你需要显示非常大的文本,可能需要考虑使用WebView而不是TextView。WebView没有字符限制,但它的性能开销可能会稍大一些。

3、使用Assets:
在这里插入图片描述


    private String getTextFromAssets() {
        try {
            AssetManager assetManager = this.getResources().getAssets();
            InputStream inputStream = assetManager.open("disclaimer_content.txt");
            byte[] data = new byte[inputStream.available()];
            inputStream.read(data);
            String largeText = new String(data, "UTF-8");
            return largeText.replace("\\n", "\n");
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }

    }
    public  String getStringFromAssets() {
        String str = "";
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(this.getResources().getAssets().open("disclaimer_content.txt"), "UTF-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            StringBuilder stringBuilder = new StringBuilder();
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
            bufferedReader.close();
            inputStreamReader.close();
            str = stringBuilder.toString().replace("\\n", "\n");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }


  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值