如何从图库中获取图像或从相机中获取图像并将其插入到编辑文本中?

how to get an image from gallery or take from camera and insert it into an edittext?

我是 Android 编程新手。 我想做的是从图库中获取图像或从相机中获取图像,然后插入到可能带有文本的编辑文本中。 (例如,文本....文本...文本.. [图像])

获取图像并将其插入到编辑文本的选定光标点的代码应该如何?

你可以用两种方法做到这一点

1)与SpannableString

 SpannableString ss = new SpannableString("abc\n");
    Drawable d = img.getDrawable();
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
    ss.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    editText.setText(ss);

2) 与 CompoundDrawablesWithIntrinsicBounds

editText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.image, 0, 0, 0);

尝试以编程方式执行此操作:

EditText et_EditImage = (EditText)findViewById(R.id.et_EditImage);
et_EditImage.setCompoundDrawables(null, null, getResources().getDrawable(R.drawable.tick), null);

在 XML 文件中尝试这样做:

<FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <EditText
                android:id="@+id/search"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/search_bar"
                android:drawablePadding="8dp"
                android:paddingLeft="30dp"
                android:paddingRight="10dp"
                android:singleLine="true" >
                <requestFocus />
            </EditText>

            <Button
                android:id="@+id/searchBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left|center_vertical"
                android:layout_margin="10dp"
                android:background="@drawable/icon_magnify" />

            <Button
                android:id="@+id/delete"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|center_vertical"
                android:layout_margin="8dp"
                android:background="@drawable/icon_remove" />
        </FrameLayout>

我发现 Here