【问题标题】:Android Asset to RawAndroid 资源转为 Raw
【发布时间】:2012-12-04 19:08:05
【问题描述】:

我正在尝试本地化我的应用程序,以便它同时显示西班牙语和英语。我已经弄清楚了大部分本地化,但有一个例外。我有一个 .xml 文件,其中列出了我想移动到 res/raw 文件夹的应用程序问题,以便我可以添加语言环境并让应用程序翻译。

在大多数情况下,这很容易,因为我已经能够使用 (getResources().getString(R.string."") 从 /res 调用大多数字符串但是我很难为我的当前参数。

我的 xml 位于 res/raw/"".xml

我现在正在使用资产管理器来获取问题的 .xml

 public static List<Question> readQuestions(AssetManager assetManager){

    ArrayList<Question> questionArrayList = new ArrayList<Question>();
    try{
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(assetManager.open("questions.xml"));
        doc.getDocumentElement().normalize();
        NodeList nodeLst = doc.getElementsByTagName("question");
        for(int s=0; s<nodeLst.getLength(); s++){

有人知道我可以调用原始文件夹以获取 .xml 的方法吗?

编辑:::

我正在尝试将代码用于输入流,但在上下文中出现错误,提示无法解析符号“上下文”

 public static List<Question> readQuestions(AssetManager assetManager){

    ArrayList<Question> questionArrayList = new ArrayList<Question>();
    try{
        InputStream in = context.getResources().openRawResource(R.raw.questions);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        Document doc = builder.parse(in);


        NodeList nodeLst = doc.getElementsByTagName("question");

【问题讨论】:

    标签: java android


    【解决方案1】:

    获取 res/raw/questions.xml 的 InputStream

    InputStream in = context.getResources().openRawResource(R.raw.questions);
    

    这个答案假设你有一个上下文。如果您不熟悉在 Android 中获取和使用 Context,我强烈建议您花时间真正理解它。否则你不会在 Android 编程中走得太远。

    StackOverflow 上有很多答案已经在讨论上下文。这是一个What is 'Context' on Android?

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 2011-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多