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

以下代码throws成为FileNotFound异常:

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

但是这行代码有效:
InputStream stream = res.openRawResource (R.raw.myfile);

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

最佳答案

您可以这样操作:

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

不需要try/catch块。

10-07 15:36