I want to open a file from the folder res/raw/. I am absolutely sure that the file exists. To open the file I have tried
File ddd = new File("res/raw/example.png");
The command
ddd.exists();
yields FALSE. So this method does not work.
Trying
MyContext.getAssets().open("example.png");
ends up in an exception with getMessage() "null".
Simply using
R.raw.example
is not possible because the filename is only known during runtime as a string.
Why is it so difficult to access a file in the folder /res/raw/ ?
Here is example of taking XML file from raw folder:
Then you can:
when:
With the help of the given links I was able to solve the problem myself. The correct way is to get the resource ID with
To get it as a InputStream
You can read files in raw/res using
getResources().openRawResource(R.raw.myfilename)
.BUT there is an IDE limitation that the file name you use can only contain lower case alphanumeric characters and dot. So file names like
XYZ.txt
ormy_data.bin
will not be listed in R.