安卓:字符编码原始资源文件 [英] Android: Character encoding raw resource files

查看:110
本文介绍了安卓:字符编码原始资源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在翻译我的应用程序之一,西班牙的过程中,和我有一个字符编码的问题,一个原始的HTML文件,我坚持到web视图。我有一个文件在我的原-ES文件夹中的西班牙语翻译,而我用下面的函数读取它:

I'm in the process of translating one of my apps to Spanish, and I'm having a character encoding problem with a raw HTML file I'm sticking into a WebView. I have the spanish translation of the file in my raw-es folder, and I'm reading it in with the following function:

private CharSequence getHtmlText(Activity activity) {
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.help), "utf-8"));
            String line;
            StringBuilder buffer = new StringBuilder();
            while ((line = in.readLine()) != null) buffer.append(line).append('\n');
            return buffer;
        } catch (IOException e) {
            return "";
        } finally {
            closeStream(in);
        }
    }

但处处有文件在西班牙的性格,有一个与当我运行的应用程序,并期待在显示HTML中的活动里面就一个问号钻石。我使用以下命令加载文本到web视图:

But everywhere there is a spanish character in the file, there is a diamond with a question mark inside of it when I run the app, and look at the activity that displays the HTML. I'm using the following to load the text into the WebView:

mWebView.loadData(text, "text/html", "utf-8");

我最初创建在Microsoft Word文件,所以我敢肯定有字符编码的问题,某种事,但我真的不知道如何解决它,谷歌搜索是没有帮助的。任何想法?

I originally created the file in Microsoft Word, so I'm sure there is some sort of character encoding issue going on, but I'm not really sure how to fix it, and a Google search isn't helping. Any ideas?

推荐答案

不要使用loadData。使用loadDataWithBaseURL代替。你会说:

Don't use loadData. Use loadDataWithBaseURL instead. You would say:

mWebView.loadDataWithBaseURL( null, text, "text/html", "utf-8", null );

这篇关于安卓:字符编码原始资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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