Mahdi.Pishguy Mahdi.Pishguy - 5 months ago 50
Android Question

Android insert image into EditText

I'm trying to adding some image into EditText with below code as :

Bitmap myBitmap = BitmapFactory.decodeFile(APP.DIR_APP + APP.IMAGE + "/ok.png");

Drawable mDrawable = new BitmapDrawable(getResources(), myBitmap);

ImageSpan imageSpan = new ImageSpan(mDrawable);

SpannableStringBuilder builder = new SpannableStringBuilder();

String imgId = "[img=1]";

int selStart = contentText.getSelectionStart();
builder.replace(contentText.getSelectionStart(), contentText.getSelectionEnd(), imgId);

builder.setSpan(imageSpan, selStart, selStart + imgId.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

this path as
APP.DIR_APP + APP.IMAGE + "/ok.png"
is correct and i check that with
and that return true, but i don't have selected image into edittext

Answer Source

Do not forget to call mDrawable.setBounds(0, 0, myBitmap.getWidth(), myBitmap.getHeight()). or mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());

Currently you have bounds set to 0,0,0,0 -- so, you don't see your image. It's zero-sized.

You can set other bounds if you want to scale your image.

Do not forget to scale your image for different screen densities.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download