-1

My photos on the route:

Android\data\com.gmb.myapp\photo

inside of,Custom TextView, add photos.

As in the picture below:

enter image description here

In the following code example,From drawable folder,Photos are received.(Photos, get the following route! Android\data\com.gmb.myapp\photo Not from, drawable)

Inside the text, the image looks like this:

Press [img src=myphoto/] to accept

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;
    }

}

Update :

In id the path of the drawable folder is defined.

spannable.setSpan(new ImageSpan(context,id)

I want to define the following path:

Android\data\com.gmb.myapp\photo

  • 2
    So, what's your question? – isaaaaame Jan 15 '20 at 13:55
  • The problem is, photos must be drawable in the folder. To display. Inside the drawable folder, photos cannot be added via coding. Now my photos are here: Android\data\com.gmb.myapp\photo How do we put the photo in the text? – Hamed Rahimi Jan 15 '20 at 14:11
  • So, if I understood it correctly: you get images to apps folder, but you want to use them? If that's the case: if you know the photo name and location, you can get Uri from it, and then follow [this answer](https://stackoverflow.com/a/16718935/10883621) to set the image from uri. – isaaaaame Jan 15 '20 at 14:30
  • No!.See this? spannable.setSpan(new ImageSpan(context,id), In id the path of the drawable folder is defined. I want to define the following path: Android\data\com.gmb.myapp\photo – Hamed Rahimi Jan 15 '20 at 15:33
  • Have even you [looked at ImageSpan](https://developer.android.com/reference/android/text/style/ImageSpan) ? `new ImageSpan()` **CAN** take Context and Bitmap, Drawable and String (source), **Context and Uri**, Context and resId (and any of those + vertical alignment – isaaaaame Jan 16 '20 at 07:15
  • Many thanks. The problem was solved. Using Uri. – Hamed Rahimi Jan 17 '20 at 11:54

1 Answers1

1

The problem was solved. Using Uri.

Photos, with the PNG extension, should be in the path below. Android\data\com.codinginflow.myawesomequiz\Directory_name

Class Codes:

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;
    }

}