[英]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.toHtml
和Html.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.