我有html文本,我正在将其解析为TextView。文字有图像所以我找到了imagegetter
。
imagegetter
的代码:
spanned = Html.fromHtml(content, new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
/*Toast.makeText(getApplicationContext(), source,
Toast.LENGTH_LONG).show();*/
Drawable drawable = null;
if (source.startsWith("http")) {
// load from internet
URL sourceURL;
try {
sourceURL = new URL(source);
URLConnection urlConnection = sourceURL.openConnection();
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
BufferedInputStream bufferedInputStream =
new BufferedInputStream(inputStream);
Bitmap bm = BitmapFactory.decodeStream(bufferedInputStream);
// convert Bitmap to Drawable
drawable = new BitmapDrawable(resources, bm);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
int k = width / bm.getWidth();
if(k > 2) { k = 2; }
drawable.setBounds(0, 0,
bm.getWidth() * k,
bm.getHeight() * k);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
// load from local drawable
}
return drawable;
}
}, null);
它工作正常,但是当它有URL时:
http://www.svetandroida.cz/media/2016/06/android-pro-začátečníky-velikost-SIM.jpg
它得到
java.io.FileNotFoundException: http://www.svetandroida.cz/media/2016/06/android-pro-začátečníky-velikost-SIM.jpg
我该如何解决?
答案 0 :(得分:0)
FileNotFoundException
。虽然在您的情况下页面确实存在,但我认为您因为网址中的重音字符而导致错误,即č
,á
和í
。
您需要对字符进行URL编码,以便您的网址变为:
http://www.svetandroida.cz/media/2016/06/android-pro-za%C4%8D%C3%A1te%C4%8Dn%C3%ADky-velikost-SIM.jpg