【问题标题】:openRawResourceFd fails on androidopenRawResourceFd 在 android 上失败
【发布时间】:2011-11-22 04:54:42
【问题描述】:

我正在编写我的第一个 Android 应用程序,并且正在尝试读取 res/raw 资源文件。

以下代码throws 一个FileNotFound 异常:

AssetFileDescriptor fd = res.openRawResourceFd(R.raw.myfile);

但这行代码有效:

InputStream stream = res.openRawResource (R.raw.myfile);

我需要AssetFileDescriptor 来确定文件的长度。任何想法为什么它不起作用?

【问题讨论】:

  • 你找到什么方法了吗??

标签: android file-descriptor android-assets


【解决方案1】:

你可以这样做:

FileDescriptor fd = getResources().openRawResourceFd(R.raw.rawResourceId).getFileDescriptor();

不需要 try/catch 块。

【讨论】:

    【解决方案2】:

    这行得通;

    AssetFileDescriptor afd = res.openRawResourceFd(R.raw.rawResourceId);
    

    【讨论】:

      【解决方案3】:

      将您的 myfile 移动到资产文件夹并尝试此操作

      try{
          AssetFileDescriptor descriptor = getAssets().openFd( "myfile" );
          FileDescriptor fd = descriptor.getFileDescriptor();
      }
      catch(Exception)
      {
      
      }
      

      或者您可以尝试使用以下代码从资源/原始文件打开一个 RAW 文件:

      FileDescriptor fd = getResources().openRawResourceFd(R.raw.myfile).getFileDescriptor();
      

      【讨论】:

      • 这不是永久的解决方案,因为在资产文件夹中我得到文件被压缩错误,我需要将文件放在原始目录中,只看到这个线程stackoverflow.com/questions/6186866/…
      • openRawResource 返回 Inputstream 而不是 FileInputStream
      猜你喜欢
      • 1970-01-01
      • 2022-10-23
      • 2015-08-18
      • 2018-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多