简体   繁体   中英

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

I'm new to Android programming. What I'm trying to do is get a image from the gallery or take it from the camera, and insert into the edittext which might have texts on it. (eg, text....text...text.. [IMAGE])

How should the code for getting a image and insert it into the selected cursor point of the edittext?

You can do this with 2 methods

1)With 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) With CompoundDrawablesWithIntrinsicBounds

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

Try this to do programmatically:

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

Try this to do it in XML file:

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

I found that on Here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM