我想在textview中显示图像。我在res / drawable / img.png中有一个图像 这是我的代码:
String img="<img src='file:///res/drawable/img.png' />";
txt_html.append(Html.fromHtml(img));
但这不起作用。
有什么想法吗?
答案 0 :(得分:4)
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.widget.TextView;
public class TextImageActivity extends Activity {
int imageNumber = 1; //int to check which image is displayed
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tvText = (TextView) findViewById(R.id.text);
final String testContent = "<html><body><b>Test</b><i>Italic</i><br/>"
+ "<img src=\"icon.png\"/>This is like testing if this thing works" + "<img src=\"a.png\"/>" +
" in a more elaborate</body></html>";
tvText.setText(Html.fromHtml(testContent, imgGetter, null));
}
private ImageGetter imgGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable drawable = null;
if(imageNumber == 1) {
drawable = getResources().getDrawable(R.raw.icon);
++imageNumber;
} else drawable = getResources().getDrawable(R.raw.a);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable
.getIntrinsicHeight());
return drawable;
}
};
}
答案 1 :(得分:3)
public class MainActivity extends ActionBarActivity {
public String htmlContent;
int ScreenW,ScreenH;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_news);
this.context = getBaseContext();
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float dpWidth = displayMetrics.widthPixels;
float dpHeight = displayMetrics.heightPixels;
ScreenW = (int) dpWidth;
ScreenH = (int) dpHeight;
try {
((TextView) findViewById(R.id.tv_my_content)).setText(Html.fromHtml(htmlContent, Images, null));
}
catch (Exception ex)
{
}
}
private Html.ImageGetter Images = new Html.ImageGetter() {
public Drawable getDrawable(String source) {
Drawable drawable = null;
FetchImageUrl fiu = new FetchImageUrl(context,source);
try {
fiu.execute().get();
drawable = fiu.GetImage();
}
catch (Exception e) {
drawable = getResources().getDrawable(R.drawable.img_news);
}
// to display image,center of screen
int imgH = drawable.getIntrinsicHeight();
int imgW = drawable.getIntrinsicWidth();
int padding =20;
int realWidth = ScreenW-(2*padding);
int realHeight = imgH * realWidth/imgW;
drawable.setBounds(padding,0,realWidth ,realHeight);
return drawable;
}
};
}
public class FetchImageUrl extends AsyncTask<String, String, Boolean> {
String imageUrl;
Context context;
protected Drawable image;
public FetchImageUrl(Context context, String url)
{
this.imageUrl = url;
image = null;
this.context = context;
}
public Drawable GetImage()
{
return image;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(String... args) {
try {
InputStream input_stream = (InputStream) new URL(imageUrl).getContent();
image = Drawable.createFromStream(input_stream, "src name");
return true;
}
catch (Exception e)
{
image = null;
}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
}}