2017-10-08 95 views
1

我得到除了当我尝试打开res/raw文件夹中的文件。Android开发,原始资源无法找到

InputStream in = Resources.getSystem().openRawResource(R.raw.eng_sample); 

唯一的例外是:

android.content.res.Resources$NotFoundException: Resource ID #0X89890 

我可以看到该文件eng_sample.txt在APK中res/raw/eng_sample.txt

最重要的是,当我做R.raw.并按Ctrl-space文件eng_sample作为建议给出。

然后是什么问题?我该如何解决?

非常感谢提前。

P.S.

我也尝试把文件eng_sample.txt纳入main/assets。然后提取文件与

InputStream in = context.getAssets().open("file:///android_asset/eng_sample.txt 

但现在我得到一个IO异常。

也许有人可以告诉我如何使这种方法工作,而不是上面的那个?

回答

1

Resources.getSystem()返回共享全局资源对象,该对象仅提供对系统资源(无应用程序资源)的访问。

另外context.getAssets().open(fileName/*not path*/)用于从资产目录中获取文件,您应该只提供文件名,而不是整个路径。

要从原始文件打开流,使用方法:

context.getResources().openRawResource(R.raw.eng_sample); 
1

我一直在使用同样的目的该代码。你可以做这样的事情

BufferedReader mReader = null; 
try { 
     mReader = new BufferedReader(
    new InputStreamReader(getAssets().open("eng_sample.txt"))); 
String mLine; 
while ((mLine = mReader.readLine()) != null) { 
     //do your stuff with line 
     ... 
    } 
    } catch (IOException e) { 
    //print stack trace 
    } finally { 
    if (mReader != null) { 
      try { 
        mReader.close(); 
       } catch (IOException e) { 
       //print stack trace 
      } 
     } 
    } 

希望它有帮助。