简体   繁体   English

在webview android中加载文本文件

[英]load textfile in webview android

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. 到目前为止,我已经阅读了如何在webview中加载“普通”html网页..到目前为止,我传递了包含我的文本文件路径的URL,但它没有加载任何内容。 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: 但继续得到这个屏幕: webviewerror

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. 所以任何帮助将不胜感激。

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(...) . 尝试阅读文本文件的内容,并在文本前加上<html><body>然后追加</body></html>然后使用WebView方法loadData(...)

Example: 例:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM