11package com .swmansion .enriched .utils ;
22
3+ import android .content .res .Resources ;
4+ import android .graphics .Bitmap ;
5+ import android .graphics .BitmapFactory ;
6+ import android .graphics .Color ;
7+ import android .graphics .drawable .BitmapDrawable ;
8+ import android .graphics .drawable .ColorDrawable ;
39import android .graphics .drawable .Drawable ;
410import android .text .Editable ;
511import android .text .Layout ;
@@ -275,6 +281,7 @@ private static void withinParagraph(StringBuilder out, Spanned text, int start,
275281 if (style [j ] instanceof EnrichedImageSpan ) {
276282 out .append ("<img src=\" " );
277283 out .append (((EnrichedImageSpan ) style [j ]).getSource ());
284+ out .append ("\" " );
278285
279286 out .append (" width=\" " );
280287 out .append (((EnrichedImageSpan ) style [j ]).getWidth ());
@@ -697,6 +704,10 @@ private static void startImg(Editable text, Attributes attributes, EnrichedParse
697704 d = img .getDrawable (src );
698705 }
699706
707+ if (d == null ) {
708+ d = HtmlToSpannedConverter .prepareDrawableForImage (src );
709+ }
710+
700711 if (d == null ) {
701712 return ;
702713 }
@@ -706,6 +717,30 @@ private static void startImg(Editable text, Attributes attributes, EnrichedParse
706717 text .setSpan (new EnrichedImageSpan (d , src , Integer .parseInt (width ), Integer .parseInt (height )), len , text .length (), Spannable .SPAN_EXCLUSIVE_EXCLUSIVE );
707718 }
708719
720+ private static BitmapDrawable prepareDrawableForImage (String src ) {
721+ String cleanPath = src ;
722+ if (cleanPath .startsWith ("file://" )) {
723+ cleanPath = cleanPath .substring (7 );
724+ }
725+
726+ BitmapDrawable drawable = null ;
727+
728+ try {
729+ Bitmap bitmap = BitmapFactory .decodeFile (cleanPath );
730+ if (bitmap != null ) {
731+ drawable = new BitmapDrawable (Resources .getSystem (), bitmap );
732+ // set bounds so it knows how big it is naturally,
733+ // though EnrichedImageSpan will override this with the HTML width/height later.
734+ drawable .setBounds (0 , 0 , bitmap .getWidth (), bitmap .getHeight ());
735+ }
736+ } catch (Exception e ) {
737+ // Failed to load file
738+ android .util .Log .e ("EnrichedParser" , "Failed to load image from path: " + cleanPath , e );
739+ }
740+
741+ return drawable ;
742+ }
743+
709744 private static void startA (Editable text , Attributes attributes ) {
710745 String href = attributes .getValue ("" , "href" );
711746 start (text , new Href (href ));
0 commit comments