Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openRawResourceFd fails on android

I'm writing my first Android application, and I'm trying to read a res/raw resource file.

The following code throws a FileNotFound Exception:

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

but this line of code works:

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

I need the AssetFileDescriptor in order to determine the length of the file. Any ideas why it isn't working?

like image 281
WOPR Avatar asked Nov 22 '11 04:11

WOPR


2 Answers

You can do it this way:

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

No try/catch block required.

like image 133
Chepech Avatar answered Oct 04 '22 15:10

Chepech


This works;

AssetFileDescriptor afd = res.openRawResourceFd(R.raw.rawResourceId);
like image 24
Jessicardo Avatar answered Oct 04 '22 16:10

Jessicardo