简体   繁体   English

如何将带有文本的 ImageSpan 保存到 SQL Lite 数据库中?

[英]How to Save ImageSpan with text into SQL Lite Database?

I have implemented image in EditEtxt.我已经在 EditEtxt 中实现了图像。 Its working fine.它工作正常。 I want to save the whole editText entities(Text & image) in sql lite database.我想将整个 editText 实体(文本和图像)保存在 sql lite 数据库中。 When i save it to sql only text are showing not images.当我将其保存到 sql 时,仅显示文本而不显示图像。 I also tried with Html.toHtml and Html.fromHtml .我还尝试了Html.toHtmlHtml.fromHtml Its also not working.它也不起作用。

This bunch of code to add image in editText:这一堆在editText中添加图像的代码:

            Uri imageUri= data.getData();
            InputStream inputStream= getContentResolver().openInputStream(imageUri);
            Bitmap bitmap= BitmapFactory.decodeStream(inputStream);
           
            final Drawable drawable = new BitmapDrawable(getResources(), bitmap);
            drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
            final ImageSpan imageSpan = new ImageSpan(drawable,ImageSpan.ALIGN_BOTTOM);

            SpannableStringBuilder span = new SpannableStringBuilder(editText.getText()+".\n");
            span.setSpan(imageSpan, 0, (".\n").length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            editText.append(span, 0, (".\n").length());

To save in databse保存在数据库中

                    String textWithImage= Html.toHtml(editText.getText());
                        Note note = new Note(textWithImage);
                        Databse databse= new Databse(this);
                        databse.insertInDB(note);
              

Fetch data from database从数据库中获取数据

databse= new Databse(this);
    note= databse.getSingleNote(noteId);
    String text= note.getNoteText();
    editText.setText(Html.fromHtml(text));

Please help someone to do this task.请帮助某人完成此任务。 Thank you.谢谢你。

You need a way to save the URI into the database.您需要一种将 URI 保存到数据库中的方法。 Your approach with Html.toHtml() is valid, but you are not linking the Uri to the ImageSpan and are, probably, getting a null in the src attribute.您使用Html.toHtml()的方法是有效的,但是您没有将 Uri 链接到ImageSpan并且可能在src属性中获得null Try another ImageSpan constructor:尝试另一个ImageSpan构造函数:

ImageSpan (Drawable drawable, String source, int verticalAlignment) ImageSpan (Drawable drawable, String source, int verticalAlignment)

where source is your Uri string.其中source是您的 Uri 字符串。

Now when you call Html.toHtml() , the src attribute of the img tag will contain the URI.现在,当您调用Html.toHtml()时, img标签的src属性将包含 URI。 To interpret the src attribute, you will need to supply an Html.ImageGetter which will look something like this:要解释src属性,您需要提供一个看起来像这样的Html.ImageGetter

// This function is used by HtmlCompate.fromHtml to retrieve drawables for the img tag.
override fun getDrawable(source: String?): Drawable {
    val uri = Uri.parse(source)
    val stream = contentResolver.openInputStream(uri)
    val bitmap = BitmapFactory.decodeStream(stream)
    val drawable = BitmapDrawable(resources, bitmap)
    drawable.setBounds(0, 0, bitmap.width, bitmap.height)
    return drawable
}

Here, source is the URI for your drawable.在这里,source 是你的 drawable 的 URI。 (This is Kotlin, but it is close enough to the Java to understand.) (这是 Kotlin,但它与 Java 足够接近,可以理解。)

This will not store your bitmap drawable in the database and assumes that the drawable is at the same URI when the data is read from the database.这不会将您的位图可绘制对象存储在数据库中,并假定从数据库读取数据时可绘制对象位于相同的 URI。

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

相关问题 Android - ImageSpan - 如何在文本末尾居中对齐图像 - Android - ImageSpan - How to center align the image at the end of the text 文本在EditText中与ImageSpan混淆 - Text is messed up with ImageSpan in EditText 在 jetpack compose Text 中添加 ImageSpan 的替代方法 - alternative way for adding ImageSpan in jetpack compose Text 如何在 Android 中为 ImageSpan 应用背景颜色? - How to apply the background color for the ImageSpan in Android? 有什么方法可以在不破坏文本的情况下在 TextView 中插入 ImageSpan? - Is there any way to insert an ImageSpan in a TextView without disrupting the text? 使用SpannableStringBuilder添加图像跨度后,如何防止光标在EditText(MultiAutoCompleteTextView)中调整大小? - How can I prevent the cursor from resizing in an EditText (MultiAutoCompleteTextView) after I add an imagespan using a SpannableStringBuilder? 将背景添加到 ImageSpan - Add Background to ImageSpan 将 ImageSpan 对齐到 TextView 的顶部 - Aligning ImageSpan to the top of the TextView 降低ImageSpan的高度和宽度 - Reduce ImageSpan height and width 为什么将 ImageSpan 添加到 Snackbar 的操作文本在 Android 设备 SDK 级别 26 上有效,但在 SDK 级别 25 上无效? - Why does adding an ImageSpan to a Snackbar's action text work on Android devices SDK level 26 but not on SDK level 25?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM