2

我在 Android N 上遇到了这个异常: java.lang.StringIndexOutOfBoundsException 行: res.add(new SpannableStringBuilder(in.subSequence(lastImageSpanPosition, spanStart))); 我不知道为什么android N中的fromHtml方法返回不同的值

这些是我的代码:

        ArrayList<Object> res = new ArrayList<Object>();
        Spanned in;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            in = Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY);
        } else {
            in = Html.fromHtml(html);
        }
        Object[] spans = in.getSpans(0, Integer.MAX_VALUE, Object.class); // get all spans
        int lastImageSpanPosition = 0; // it's end position of image span
        for (int i = 0; i < spans.length; i++) { // itarete searching ImageSpan
            Object span = spans[i];

            if (span instanceof ImageSpan) {
                int spanStart = in.getSpanStart(span); // If you;ve found one

                res.add(new SpannableStringBuilder(in.subSequence(lastImageSpanPosition, spanStart))); // add all previous spans as a single Spannable object

                ImageSpan imageSpan = (ImageSpan) span;
                String imageUrl = imageSpan.getSource();
                if (imageUrl != null && !imageUrl.startsWith("http"))
                    imageUrl = "http:" + imageUrl;

                if (imageUrl != null) {
                    res.add(new ImageSpan(null, imageUrl)); // add separate span for image
                }
                lastImageSpanPosition = in.getSpanEnd(span);
            }
        }
4

0 回答 0