在web视图Android的负载文本文件 [英] load textfile in webview android

查看:88
本文介绍了在web视图Android的负载文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经阅读如何加载在一个web视图正常的HTML网页.. 到目前为止,我传递一个包含我的文本文件的路径的网址,但它加载什么。 这是我的方法:

so far i have read how to load "normal" html webpages in a webview .. so far I pass the URL containing the path of my text file but it loads nothing. this is my method:

@Override
public void onSelected(String url) {
    ViewerFragment viewer = (ViewerFragment) getSupportFragmentManager()
            .findFragmentById(R.id.view_fragment);

    if (viewer == null || !viewer.isInLayout()) {
        Intent showContent = new Intent(getApplicationContext(),
                ViewerFragment.class);
        showContent.setData(Uri.parse(url));
        startActivity(showContent);
    } else {
        viewer.updateUrl(url);
    }

}

和观众的片段得到这样的:

and the viewer fragment got this:

    public class ViewerFragment extends Fragment{
    private WebView viewer = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        viewer = (WebView) inflater
                .inflate(R.layout.details_fragment, container, false);
        return viewer;
    }

    public void updateUrl(String newUrl) {
        if (viewer != null) {
            viewer.loadUrl(newUrl);
        }
    }
    }

但不断收到这样的屏幕:

but keep getting this screen:

任何想法如何做到这一点? = /我已经尝试过谷歌上搜索了一下,但没有找到关于它的很多信息...居然发现几乎没有。因此,任何帮助将是AP preciated。

any ideas how to do this? =/ I already tried googling a bit but didnt find much info about it... actually found almost none. So any help would be appreciated.

推荐答案

尝试阅读的文本文件和$ P $的pfixing与&LT文本内容; HTML><身体GT; 然后追加< / BODY>< / HTML> 然后使用的WebView 方法 loadData(...)

Try reading the contents of the text file and prefixing the text with <html><body> then append </body></html> then use the WebView method loadData(...).

例如:

StringBuilder sb = new StringBuilder("<html><body>");
sb.append(readTextFile());
sb.append("</body></html>");
myWebView.loadData(sb.ToString(), "text/html", "UTF-8");

public String readTextFile(String filename) {
    // Open and read the contents of <filename> into
    // a single string then return it
}

这篇关于在web视图Android的负载文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆