0

I am new to java.
I have a directory with a txt file (R.raw). I want to get access with a command

InputStream in_s = res.openRawResource(R.raw.itemname);

where itemname is a dynamic string with a filename from a previous activity.

How do I can get open file in R.raw by a string "n0.txt"?

In javascript I can implement it like R.raw["n0.txt"] or R.raw.itemname.

Thanks in advance.

user2560165
  • 149
  • 1
  • 2
  • 15

1 Answers1

0

You need to use method getIdentifier().

Context context;
Resources res;

int itemId = res.getIdentifier("itemname", "raw", context.getPackageName());
InputStream is = res.openRawResource(itemId);

Original post here.

Community
  • 1
  • 1
Oleksii K.
  • 5,359
  • 6
  • 44
  • 72