Skip to content Skip to sidebar Skip to footer

Is It Possible To Use A String To Call A File In Raw In Java?

Is it possible to create a string like public String pos1='bloodstone'; And then use it in something like Mediaplayer as: Mediaplayer s1;

Solution 1:

Use getIdentifier():

Stringpos1="bloodstone";

intrawId= getResources().getIdentifier(pos1, "raw", getPackageName());
Mediaplayers1= MediaPlayer.create(this, rawId);

This code snippet assumes that you are in a method on the activity — getResources() and getPackageName() are methods on Context, if your code resides somewhere outside of an activity.

Post a Comment for "Is It Possible To Use A String To Call A File In Raw In Java?"