给出以下HTML:
This is text and this is an image .
是否可以渲染图像?当使用这个代码片段时mContentText.setText(Html.fromHtml(text));
,我得到一个带有黑色边框的青色盒子,让我相信TextView对img标签是什么有所了解.
如果您查看文档,Html.fromHtml(text)
您会看到它说:
HTML中的任何标记都将显示为一般替换图像,然后您的程序可以通过该图像进行替换并替换为真实图像.
如果你不想自己做这个替换,你可以使用另一个Html.fromHtml()
方法,它接受一个Html.TagHandler
和一个Html.ImageGetter
参数以及要解析的文本.
在你的情况下你可以解析null
,Html.TagHandler
但你需要实现自己的,Html.ImageGetter
因为没有默认的实现.
但是,您将遇到的问题是Html.ImageGetter
需要同步运行,如果您从Web下载图像,则可能需要异步执行此操作.如果您可以添加要在应用程序中显示为资源的任何图像,则ImageGetter
实现变得更加简单.你可以逃脱:
private class ImageGetter implements Html.ImageGetter { public Drawable getDrawable(String source) { int id; if (source.equals("stack.jpg")) { id = R.drawable.stack; } else if (source.equals("overflow.jpg")) { id = R.drawable.overflow; } else { return null; } Drawable d = getResources().getDrawable(id); d.setBounds(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight()); return d; } };
您可能想要找出更聪明的东西来将源字符串映射到资源ID.
我已经在我的应用程序中实现了,从pskink .thanx中获取了很多
package com.example.htmltagimg; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.LevelListDrawable; import android.os.AsyncTask; import android.os.Bundle; import android.text.Html; import android.text.Html.ImageGetter; import android.text.Spanned; import android.util.Log; import android.widget.TextView; public class MainActivity extends Activity implements ImageGetter { private final static String TAG = "TestImageGetter"; private TextView mTv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String source = "this is a test of ImageGetter it contains " + "two images:
" + "
and
" + ""; String imgs="Test Article, Test Article, Test Article, Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,v
"; String src="Test Attractions Test Attractions Test Attractions Test Attractions
"; String img="Test Article, Test Article, Test Article, Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,v
"; Spanned spanned = Html.fromHtml(imgs, this, null); mTv = (TextView) findViewById(R.id.text); mTv.setText(spanned); } @Override public Drawable getDrawable(String source) { LevelListDrawable d = new LevelListDrawable(); Drawable empty = getResources().getDrawable(R.drawable.ic_launcher); d.addLevel(0, 0, empty); d.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight()); new LoadImage().execute(source, d); return d; } class LoadImage extends AsyncTask
根据以下@rpgmaker评论我添加了这个答案
是的,你可以使用ResolveInfo类
检查您的文件是否已支持已安装的应用程序
使用以下代码:
private boolean isSupportedFile(File file) throws PackageManager.NameNotFoundException { PackageManager pm = mContext.getPackageManager(); java.io.File mFile = new java.io.File(file.getFileName()); Uri data = Uri.fromFile(mFile); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(data, file.getMimeType()); ListresolveInfos = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); if (resolveInfos != null && resolveInfos.size() > 0) { Drawable icon = mContext.getPackageManager().getApplicationIcon(resolveInfos.get(0).activityInfo.packageName); Glide.with(mContext).load("").placeholder(icon).into(binding.fileAvatar); return true; } else { Glide.with(mContext).load("").placeholder(R.drawable.avatar_defaultworkspace).into(binding.fileAvatar); return false; } }
这就是我使用的,它不需要您硬化您的资源名称,并将首先在您的应用程序资源中查找可绘制资源,然后在库存android资源中找不到任何内容 - 允许您使用默认图标等.
private class ImageGetter implements Html.ImageGetter { public Drawable getDrawable(String source) { int id; id = getResources().getIdentifier(source, "drawable", getPackageName()); if (id == 0) { // the drawable resource wasn't found in our package, maybe it is a stock android drawable? id = getResources().getIdentifier(source, "drawable", "android"); } if (id == 0) { // prevent a crash if the resource still can't be found return null; } else { Drawable d = getResources().getDrawable(id); d.setBounds(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight()); return d; } } }
哪个可以这样使用(例子):
String myHtml = "This will display an image to the right "; myTextview.setText(Html.fromHtml(myHtml, new ImageGetter(), null);
我遇到了同样的问题,我找到了一个非常干净的解决方案:在Html.fromHtml()之后,您可以运行AsyncTask迭代所有标签,获取图像然后显示它们.
在这里你可以找到一些你可以使用的代码(但它需要一些自定义):https://gist.github.com/1190397