So Im using ImageGetter to display the images from JSON blog posts. Im getting the correct source in the log but the URL changes when it reaches setBounds. Any ideas?
Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blog_view);
Intent intent = getIntent();
Uri blogUri = intent.getData();
mPost = blogUri.toString();
mUrl = getIntent().getStringExtra("mUrl");
TextView textView = (TextView) findViewById(R.id.scrollView1);
textView.setText(Html.fromHtml(mPost, imgGetter, null));
}
private ImageGetter imgGetter = new ImageGetter(){
@Override
public Drawable getDrawable(String source){
Drawable drawable = Drawable.createFromPath(source);
try {
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
}catch (NullPointerException e){
logException(e);
}
return drawable;
}
};
The "source" before the try is
http://www.domain.com/images_blog/feature.png
but in the catch the error is:
Unable to decode stream:
java.io.FileNotFoundException: /http:/www.domain.com/images_blog/feature.png : open failed: ENOENT (No such file or directory)
An alternative solution using Glide
and Coroutines
with the assumption that a retry is not required:
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.text.Html
import android.widget.TextView
import androidx.lifecycle.LifecycleCoroutineScope
import com.bumptech.glide.RequestManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlin.math.roundToInt
class HtmlImageGetter(
private val scope: LifecycleCoroutineScope,
private val res: Resources,
private val glide: RequestManager,
private val htmlTextView: TextView
) : Html.ImageGetter {
override fun getDrawable(url: String): Drawable {
val holder = BitmapDrawablePlaceHolder(res, null)
scope.launch(Dispatchers.IO) {
runCatching {
val bitmap = glide
.asBitmap()
.load(url)
.submit()
.get()
val drawable = BitmapDrawable(res, bitmap)
val scale = 1.25 // This makes the image scale in size.
val width = (drawable.intrinsicWidth * scale).roundToInt()
val height = (drawable.intrinsicHeight * scale).roundToInt()
drawable.setBounds(0, 0, width, height)
holder.setDrawable(drawable)
holder.setBounds(0, 0, width, height)
withContext(Dispatchers.Main) { htmlTextView.text = htmlTextView.text }
}
}
return holder
}
internal class BitmapDrawablePlaceHolder(res: Resources, bitmap: Bitmap?) : BitmapDrawable(res, bitmap) {
private var drawable: Drawable? = null
override fun draw(canvas: Canvas) {
drawable?.run { draw(canvas) }
}
fun setDrawable(drawable: Drawable) {
this.drawable = drawable
}
}
}
In a Fragment
or Activity
use with HtmlCompat
val imageGetter = HtmlImageGetter(lifecycleScope, resources, glide, htmlTextView)
val styledText = HtmlCompat.fromHtml(htmlString, flags, imageGetter, null)
htmlTextView.text = styledText
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With