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.
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 anullin thesrcattribute. Try another ImageSpan constructor:ImageSpan (Drawable drawable, String source, int verticalAlignment)
where
sourceis your Uri string.Now when you call
Html.toHtml(), thesrcattribute of theimgtag will contain the URI. To interpret thesrcattribute, you will need to supply an Html.ImageGetter which will look something like this: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.