I have a textview in which I am showing content of a forum post, which is entered on website using rte, the content involves images, both of type web url and of type B
I just wanted to fix the size of the image from Rajat's answer, the image will take the full width of the textview and keeps the aspect ratio for the height. here's my update:
public Drawable getDrawable(String source)
{
if(source.matches("data:image.*base64.*"))
{
String base_64_source = source.replaceAll("data:image.*base64", "");
byte[] data = Base64.decode(base_64_source, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Drawable image = new BitmapDrawable(context.getResources(), bitmap);
float ratio = container.getWidth() / image.getIntrinsicWidth();
int width = container.getWidth();
int height = Math.round(image.getIntrinsicHeight() * ratio);
image.setBounds(0, 0, width, height);
return image;
}
else
{
....
}
}
public class ImageGetterAsyncTask extends AsyncTask
{
....
@Override
protected void onPostExecute(Drawable result)
{
if(result != null)
{
float ratio = container.getWidth() / result.getIntrinsicWidth();
int width = container.getWidth();
int height = Math.round(result.getIntrinsicHeight() * ratio);
urlDrawable.setBounds(0, 0, width, height); //set the correct bound according to the result from HTTP call
urlDrawable.drawable = result; //change the reference of the current drawable to the result from the HTTP call
URLImageParser.this.container.invalidate(); //redraw the image by invalidating the container
}
}
public Drawable fetchDrawable(String urlString)
{
try
{
InputStream is = (InputStream) new URL(urlString).getContent();
Bitmap bmp = BitmapFactory.decodeStream(is);
Drawable drawable = new BitmapDrawable (context.getResources(), bmp);
float ratio = container.getWidth() / drawable.getIntrinsicWidth();
int width = container.getWidth();
int height = Math.round(drawable.getIntrinsicHeight() * ratio);
drawable.setBounds(0, 0, width, height);
return drawable;
}
catch (Exception e)
{
return null;
}
}
}
}
However, Ranjat's solution shows only 1 image. If you wanna show multiple images then you need to use ImageSpan
inside SpannableStringBuilder
if you need an example let me know in a comment.