【问题标题】:Android dev, raw resource can't be foundAndroid开发,找不到原始资源
【发布时间】:2017-10-08 02:23:36
【问题描述】:

除非我尝试打开 res/raw 文件夹中的文件,否则我会得到。

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

例外是:

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

我可以在res/raw/eng_sample.txt的apk中看到文件eng_sample.txt

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

那有什么问题呢?我该如何解决这个问题?

提前致谢。

附言

我还尝试将文件eng_sample.txt 放入main/assets。然后使用

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

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

也许有人可以告诉我如何使用这种方法而不是上面的方法?

【问题讨论】:

    标签: android inputstream


    【解决方案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
               }
           }
       }
    

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-18
        • 1970-01-01
        • 2013-10-09
        • 1970-01-01
        • 2015-07-12
        • 1970-01-01
        • 2011-08-18
        相关资源
        最近更新 更多