Android: How to add R.raw to project?

Viewed 138763

How I could add R.raw class to my android project? Now I haven't /res/raw folder at all (and class as well). And I can't find any tools in eclipse to do that. So how to add R.raw?

14 Answers

You may have to restart android studio if above solutions aren't working, i restarted it and then it works.

  • You need to right-click on res->new->Android resource directory. Make sure you select directory and not file.

  • Select raw in resource type and it automatically selects raw as the directory name.

  • Drag and drop your .mp3 music file inside the res folder. Make sure that it starts with a small letter.

Here is the string to access files in raw folder. Replace filename with name of your file.

android.resource://" + getPackageName() + "/" + R.raw.filename

Create a raw android resource directory. enter image description here

Once the raw directory is created, Make sure to add a valid media file

  1. Make sure to follow proper naming conventions (lower case letters with underscore '_' as separators)
  2. Media file should have a proper encoding and formatted media file in one of the supported formats.

After following the above procedure, you should be able to access your media files by using R.raw.media_file

Right CLick on res folder -> New Android Resource Directory - select resource type raw -> ok and move your file to this folder

Even if its showing unresolved reference: raw in the Android Studio, it does still run. when I did the rebuild it didn't show any error during the build. just put the right path like R.raw.myrawfile and run it, the app will run.

if u want the red mark on the code to go away you probably have to restart (do invalidate cache and restart)

Related