【问题标题】:TextView With Images(Not from, drawable)带有图像的文本视图(不是来自,可绘制)
【发布时间】:2020-01-15 13:52:11
【问题描述】:

我在路线上的照片:

Android\data\com.gmb.myapp\photo

里面,自定义TextView,添加照片。

如下图:

在以下代码示例中,从drawable文件夹中,接收到照片。(照片,获取以下路由!Android\data\com.gmb.myapp\photo不是来自,可绘制)

在文本内部,图像如下所示:

按 [img src=myphoto/] 接受

public class TextViewWithImages extends androidx.appcompat.widget.AppCompatTextView {

    public TextViewWithImages(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public TextViewWithImages(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public TextViewWithImages(Context context) {
        super(context);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        Spannable s = getTextWithImages(getContext(), text);
        super.setText(s, BufferType.SPANNABLE);
    }

    private static final Spannable.Factory spannableFactory = Spannable.Factory.getInstance();

    private static boolean addImages(Context context, Spannable spannable) {
        Pattern refImg = Pattern.compile("\\Q[img src=\\E([a-zA-Z0-9_]+?)\\Q/]\\E");
        boolean hasChanges = false;

        Matcher matcher = refImg.matcher(spannable);
        while (matcher.find()) {
            boolean set = true;
            for (ImageSpan span : spannable.getSpans(matcher.start(), matcher.end(), ImageSpan.class)) {
                if (spannable.getSpanStart(span) >= matcher.start()
                        && spannable.getSpanEnd(span) <= matcher.end()
                ) {
                    spannable.removeSpan(span);
                } else {
                    set = false;
                    break;
                }
            }
            String resname = spannable.subSequence(matcher.start(1), matcher.end(1)).toString().trim();
            int id = context.getResources().getIdentifier(resname, "drawable", context.getPackageName());
            if (set) {
                hasChanges = true;
                spannable.setSpan(new ImageSpan(context,id),
                        matcher.start(),
                        matcher.end(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE

                );
            }
        }

        return hasChanges;
    }
    private static Spannable getTextWithImages(Context context, CharSequence text) {
        Spannable spannable = spannableFactory.newSpannable(text);
        addImages(context, spannable);
        return spannable;
    }

}

更新:

在id中定义了drawable文件夹的路径。

spannable.setSpan(new ImageSpan(context,id)

我要定义如下路径:

Android\data\com.gmb.myapp\photo

【问题讨论】:

  • 那么,你的问题是什么?
  • 问题是,文件夹中的照片必须是可绘制的。显示。在drawable文件夹内,不能通过编码添加照片。现在我的照片在这里:Android\data\com.gmb.myapp\photo 我们如何把照片放在文字里?
  • 那么,如果我理解正确的话:您将图像放到应用程序文件夹中,但您想使用它们吗?如果是这样的话:如果知道照片名称和位置,可以从中获取Uri,然后按照this answer从uri设置图片。
  • 不!看到这个了吗? spannable.setSpan(new ImageSpan(context,id), 在id中定义了drawable文件夹的路径。我想定义如下路径:Android\data\com.gmb.myapp\photo
  • 你有looked at ImageSpan吗? new ImageSpan() CAN 获取 Context 和 Bitmap、Drawable 和 String(源)、Context 和 Uri、Context 和 resId(以及任何这些 + 垂直对齐

标签: android textview imageview


【解决方案1】:

问题解决了。 使用 Uri。

带有 PNG 扩展名的照片应位于以下路径中。 Android\data\com.codinginflow.myawesomequiz\Directory_name

类代码:

public class TextViewWithImages extends androidx.appcompat.widget.AppCompatTextView {

    public TextViewWithImages(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public TextViewWithImages(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public TextViewWithImages(Context context) {
        super(context);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        Spannable s = getTextWithImages(getContext(), text);
        super.setText(s, BufferType.SPANNABLE);
    }

    private static final Spannable.Factory spannableFactory = Spannable.Factory.getInstance();

    private static boolean addImages(Context context, Spannable spannable) {
        Pattern refImg = Pattern.compile("\\Q[img src=\\E([a-zA-Z0-9_]+?)\\Q/]\\E");
        boolean hasChanges = false;

        Matcher matcher = refImg.matcher(spannable);
        while (matcher.find()) {
            boolean set = true;
            for (ImageSpan span : spannable.getSpans(matcher.start(), matcher.end(), ImageSpan.class)) {
                if (spannable.getSpanStart(span) >= matcher.start()
                        && spannable.getSpanEnd(span) <= matcher.end()
                ) {
                    spannable.removeSpan(span);
                } else {
                    set = false;
                    break;
                }
            }
            String resname = spannable.subSequence(matcher.start(1), matcher.end(1)).toString().trim();
            //int id = context.getResources().getIdentifier(resname, "drawable", context.getPackageName());

            File folder = Environment.getExternalStoragePublicDirectory("/Android/data/"+context.getApplicationContext().getPackageName()+"/"+"Directory_name"+"/");
            String fileName = folder.getPath();
            Uri uri = Uri.fromFile(new File(fileName+"/"+resname+".png"));

            if (set) {
                hasChanges = true;
                spannable.setSpan(new ImageSpan(context,uri),
                        matcher.start(),
                        matcher.end(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE

                );
            }
        }

        return hasChanges;
    }
    private static Spannable getTextWithImages(Context context, CharSequence text) {
        Spannable spannable = spannableFactory.newSpannable(text);
        addImages(context, spannable);
        return spannable;
    }

}

【讨论】:

    猜你喜欢
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多