1

I have implemented image in EditEtxt. Its working fine. I want to save the whole editText entities(Text & image) in sql lite database. When i save it to sql only text are showing not images. I also tried with Html.toHtml and Html.fromHtml. Its also not working.

This bunch of code to add image in 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.

MS Moshiur
  • 23
  • 6
  • It would be interesting to see what `Html.toHtml(editText.getText())` produces as HTML. What would the source for the image tag be? – Cheticamp Jun 25 '22 at 17:03
  • Ok, I'm just trying with Html, I didn't know it it works or not. Is there any other procedures to solve this problem? Please help me. – MS Moshiur Jun 26 '22 at 01:59

1 Answers1

1

You need a way to save the URI into the database. 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. Try another ImageSpan constructor:

ImageSpan (Drawable drawable, String source, int verticalAlignment)

where source is your Uri string.

Now when you call Html.toHtml(), the src attribute of the img tag will contain the URI. To interpret the src attribute, you will need to supply an Html.ImageGetter which will look something like this:

// 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. (This is Kotlin, but it is close enough to the Java to understand.)

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.

Cheticamp
  • 61,413
  • 10
  • 78
  • 131