I want to display html text that includes some images in a TextView
inside an activity. I am unable to show the image. I am using ImageGetter
. If I try to display local drawable it works, but when I try to fetch an image from a url it does not show my any thing.
Here is my code:
package com.example.imagegetter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
String htmlTextWithImage = "<h1>Image Getter Example</h1>"
+ "<img src=\"http://allisonnazarian.com/wp-content/uploads/2012/02/random.jpg\"/>";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(Html.fromHtml(htmlTextWithImage,new MyImageGetter(),null));
}
private class MyImageGetter implements Html.ImageGetter{
@Override
public Drawable getDrawable(String arg0) {
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(new URL(arg0).openStream());
Drawable drawable = new BitmapDrawable(bitmap);
drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
return drawable;
} catch (Exception e) {
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
d.setBounds(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight());
return d;
}
}
}
}