search

Home  >  Q&A  >  body text

android - 用textview显示html时如何写imagegetter获取网络图片

项目需要实现图文混排,后台给出来的文本是html格式的,ui要求需要调整行间距,webview可以显示各种标签,
但是无法调整行间距,试着往span中添加line-height也失败了,而且webview无法调整内边距,且webview中的内容
可以滑动,因此不太符合我们的要求
最后决定还是使用textview来实现,这样可以调整各种样式,但是在写imagegetter的时候遇到一些问题
搜索了很久,都只是搜索到一些显示本地图片没有显示网络图片,网络图片的大致方向也是要保存到本地之后再显示
但是在保存的时候会有一些问题,我保存时不知道为什么有ioexception
关于imagegetter不知道有没有什么其他的思路

private Html.ImageGetter imageGetter = new Html.ImageGetter() {
    @Override
    public Drawable getDrawable(String source) {
        String url = getApplicationContext().getExternalCacheDir().getPath() + "/image";
        File dir = new File(url);
        if (dir.exists()) {
            Drawable drawable = Drawable.createFromPath(url+source);
            if (drawable != null){
                return drawable;
            }
        }
        loadPic(source);
        return null;
    }
};
private void loadPic(final String source){
    x.image().loadDrawable(source, ImageOptions.DEFAULT,new Callback.CommonCallback<Drawable>(){
        @Override
        public void onSuccess(Drawable result) {
            super.onSuccess(result);
            saveImage(source,result,getApplicationContext());
            textview.setText(Html.fromHtml(content,imageGetter,null));
        }
    });
}
private void saveImage(String name,Drawable result, Context context) {
    Bitmap bit = ((BitmapDrawable) result).getBitmap();
    String url = context.getExternalCacheDir().getPath() + "/image";
    File dir = new File(url);
    if (!dir.exists()) {
        dir.mkdirs();
    }
    File file = new File(dir.getAbsolutePath(),name);
    if (file.exists()) {
        return url+name;
    }
    try {
        //这里会出现ioexception
        FileOutputStream fos = new FileOutputStream(file);
        bit.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
        return url+name;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

这是我的代码,不知道又没有什么其他的好方法解决

PHP中文网PHP中文网2772 days ago540

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 17:42:14

    http://git.oschina.net/zzhouj...

    I found one here, it can be used with a slight modification. Picasso needs to be configured. If I think of other ways to modify it, I will post it.

    reply
    0
  • 阿神

    阿神2017-04-17 17:42:14

    https://github.com/Sufficient...
    There is such a tool, but it is not perfect.
    Images can be automatically loaded from the network, but there is no local cache, and the loading process is asynchronous, so the height of your layout will become uncertain. It is not recommended if it is in a listview.

    reply
    0
  • 迷茫

    迷茫2017-04-17 17:42:14

    I have used this before and it is pretty good. There is also a default imagegetter solution, you can refer to it.
    https://github.com/Sufficient...

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 17:42:14

    There is another https://github.com/angebagui/...

    reply
    0
  • Cancelreply