简体   繁体   English

如何使用 spannable 或任何其他方式将多个图像添加到 EditText 中?

[英]How to add multiple image into EditText using spannable or any other way?

I am creating a note taking app, I have done text section and its working properly.我正在创建一个笔记应用程序,我已经完成了文本部分并且它工作正常。 Also database working properly.数据库也正常工作。 But I want to add image in the EditText.但我想在 EditText 中添加图像。 When user add an image, it will place at the EditText cursor position and this task will happen for multiple image.当用户添加图像时,它将放置在 EditText cursor position 并且此任务将针对多个图像发生。 And save the EditText entities (text and image) in SQL Lite database.并将 EditText 实体(文本和图像)保存在 SQL Lite 数据库中。

Please help someone to do this job.请帮助某人完成这项工作。 Thank you.谢谢你。

I have tried this job in onActivityResult, but image are not showing我在 onActivityResult 中尝试过这项工作,但没有显示图像

if(requestCode==1 && resultCode==RESULT_OK && data!=null) {
    Uri imageUri= data.getData();
    try {
        InputStream inputStream= getContentResolver().openInputStream(imageUri);
        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
        final Drawable drawable = new BitmapDrawable(getResources(), bitmap);
        final ImageSpan imageSpan = new ImageSpan(drawable,ImageSpan.ALIGN_BOTTOM);
        Spannable span = new SpannableStringBuilder(editText.getText().toString()+"\n");
        span.setSpan(imageSpan, 0, 0, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        editText.append(span, 0, ("\n").length());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

You are replacing zero characters when you do the following:当您执行以下操作时,您将替换零个字符:

span.setSpan(imageSpan, 0, 0, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

You will need to replace at least one character.您将需要替换至少一个字符。 Since you are inserting an ImageSpan , you will need to add at least one character to replace.由于您要插入ImageSpan ,因此您将需要添加至少一个字符来替换。

ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM);
Editable editable = editText.getText();
// Insert a single space to replace.
editable = editable.insert(0, " ");
editable.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setText(editable);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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