我正在尝试使用html.ImageGetter
来显示带有内嵌图像URL的html,以在TextView
中显示,代码可以正常工作,但是有两个问题需要解决,并且 如果缺少更好的方法,我正在寻找更好的选择。
1: doInBackground中的参数采用的(String) params[0]
的URL为\"http://developer.android.com/assets/images/icon_search.png\"
,但是它抛出的错误为MalformedURLException,需要通过删除反斜杠和反向来清除它逗号。
解决方法
String result = source.substring(2,source.lengeth()-2);
2 :如果图像url被urlEncoded或没有任何空格,则该代码可以正常工作;如果url包含一些空格,则参数会将其作为url直到找到第一个空格。 例如
如果url为\"http://developer.android.com/assets/images/icon search with space.png\"
String source = (String) params[0];
将网址读取为\"http://developer.android.com/assets/images/icon
解决方法
必须解析完整的html文本以查找所有图像,然后用编码的url替换图像,然后传递新的html。
String source = "this is a test of <b>ImageGetter</b> it contains two images: <br/><img src=\"http://developer.android.com/assets/images/dac_logo.png\"><br/>and<br/><img src=\"http://developer.android.com/assets/images/icon_search.png\">";
String newSource = someFunctionToEncodeAndReplaceImgSource(source);
Spanned spanned = Html.fromHtml(newSource, MyActivity.this, null);
代码
public class MyActivity extends AppCompatActivity implements Html.ImageGetter {
TextView desc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
desc = findViewById(R.id.desc);
String source = "this is a test of <b>ImageGetter</b> it contains two images: <br/><img src=\"http://developer.android.com/assets/images/dac_logo.png\"><br/>and<br/><img src=\"http://developer.android.com/assets/images/icon_search.png\">";
Spanned spanned = Html.fromHtml(source, MyActivity.this, null);
desc.setText(spanned);
}
private void handleQuestionDetails(QuestionResponseData questionAttributes) {
if (questionAttributes == null) {
AppUtils.showToast(PracticeSetAttemptActivity.this, "There is some error!", Toast.LENGTH_SHORT);
}
AppUtils.showToast(PracticeSetAttemptActivity.this, "Success", Toast.LENGTH_SHORT);
quesDescription.setText(Html.fromHtml(questionAttributes.getQuestion().getQues_description()));
nextQuesId = String.valueOf(questionAttributes.getNextId());
}
@Override
public Drawable getDrawable(String source) {
LevelListDrawable d = new LevelListDrawable();
Drawable empty = getResources().getDrawable(R.drawable.tick_icon);
d.addLevel(0, 0, empty);
d.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());
new LoadImage().execute(source, d);
return d;
}
@SuppressLint("StaticFieldLeak")
class LoadImage extends AsyncTask<Object, Void, Bitmap> {
private LevelListDrawable mDrawable;
@Override
protected Bitmap doInBackground(Object... params) {
String source = (String) params[0];
// have to replace backslash and then pass
mDrawable = (LevelListDrawable) params[1];
try {
InputStream is = new java.net.URL(source).openStream();
return BitmapFactory.decodeStream(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null) {
BitmapDrawable d = new BitmapDrawable(bitmap);
mDrawable.addLevel(1, 1, d);
mDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
mDrawable.setLevel(1);
CharSequence t = quesDescription.getText();
quesDescription.refreshDrawableState();
quesDescription.setText(t);
}
}
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
}