2014-04-25 35 views
0

是否可以从SD卡加载strings.xml而不是应用程序res/values/...在网络上搜索,但没有找到任何教程。我的想法是将xml下载到sd卡,然后将字符串元素保存到数组中。从SD卡加载strings.xml到应用程序android

public void stringsxml(){ 
     File file = new File(Environment.getExternalStorageDirectory() 
       + ".strings.xml"); 

     StringBuilder contents = new StringBuilder(); 
     try { 
       //use buffering, reading one line at a time 
       //FileReader always assumes default encoding is OK! 
       BufferedReader input = new BufferedReader(new FileReader(file)); 
       try { 
       String line = null; //not declared within while loop 
       /* 
       * readLine is a bit quirky : 
       * it returns the content of a line MINUS the newline. 
       * it returns null only for the END of the stream. 
       * it returns an empty String if two newlines appear in a row. 
       */ 
       while ((line = input.readLine()) != null){ 
        contents.append(line); 
        contents.append(System.getProperty("line.separator")); 

       } 
       } 
       finally { 
       input.close(); 
       } 
      } 
      catch (IOException ex){ 
       ex.printStackTrace(); 
      } 

      String data= contents.toString(); 

    } 

回答

0

不,这是不可能的。检查Android decoumentation关于资源:

Android SDK工具在构建时将应用程序的资源编译到应用程序二进制文件中。要使用资源,必须将其正确安装在源代码树(在项目的res /目录中)并构建应用程序。

资源内置于应用程序二进制文件中,您无法从文件中读取它们。

+1

因此而不是使用strings.xml中,我们可以存储的语言为.sqllite数据库和服务器 – Dimitri

+0

是更新数据库,但你不能把它的资源。 – Szymon

1

嗯,实际上这是半可能的,但你必须创建一个派生的LayoutInflater,它将用这样读取的字符串替换字符串代码。

我已经记录了我的尝试和失败以及初始实施here

摘要:简单的字符串工作,字符串数组做不

+0

根据你下载strings.xml比.sqllite好,反之亦然? – Dimitri