在 webview android 中加载文本文件

发布于 2024-12-24 20:09:18 字数 1263 浏览 0 评论 0原文

到目前为止,我已经阅读了如何在 webview 中加载“正常”html 网页.. 到目前为止,我传递了包含文本文件路径的 URL,但它没有加载任何内容。 这是我的方法:

@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);
    }

}

查看器片段得到了这个:

    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);
        }
    }
    }

但继续得到这个屏幕: webviewerror

有什么想法如何做到这一点? =/ 我已经尝试过谷歌搜索,但没有找到太多关于它的信息......实际上几乎没有找到。因此,任何帮助将不胜感激。

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: 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海的爱人是光 2024-12-31 20:09:18

尝试读取文本文件的内容并在文本前添加 前缀,然后附加 然后使用WebView 方法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
}

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(...).

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文