61

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?

Ksice
  • 3,277
  • 9
  • 43
  • 67
  • Thank you, guys. I've tried simply add raw folder, but it haven't any effect. And then, I don't know what exactly I did, but it worked... – Ksice Jul 14 '11 at 11:56
  • I think you were importing the wrong R class (at least that was my case). Look here: http://stackoverflow.com/questions/5884268/r-raw-anything-cannot-be-resolved – jivimberg Dec 22 '12 at 15:08

14 Answers14

61

Adding a raw folder to your resource folder (/res/) should do the trick.

Read more here: https://developer.android.com/guide/topics/resources/providing-resources.html

ThatGuyJames
  • 223
  • 2
  • 10
Stefan H Singer
  • 5,469
  • 2
  • 25
  • 26
38

enter image description hereRight click on package or Res folder click on new on popup then click on android resource directory

a new window will be appear change the resource type to raw and hit OK copy and past song to raw folder remember don't drag and drop song file to raw folder and song spell should be in lower case! This method is for Android Studio Also Check MY Link

Misbah Farooqi
  • 1,107
  • 11
  • 16
  • Note that custom name directory of "raw" type ("ssl" for example) will not be seen in the Project Panel Android View (to the left). Only "raw" is supported on the fly. – Zon May 02 '17 at 15:52
25

If you have a res/raw folder, be sure to add a file with a valid filename, otherwise the entire folder won't show up in the R class. If there's an error with a filename, it will appear in red in the console.

biegleux
  • 13,179
  • 11
  • 45
  • 52
Nebulous
  • 251
  • 3
  • 2
  • thanks, that did the trick, my mp3 file name was AudioMainAtivity, once removed the upper letters, Eclipse recognised the folder – Jorge Jul 03 '15 at 20:26
  • 3
    Also, don't forget the extension. A file without extension is considered invalid – Xwtek Sep 30 '19 at 11:11
9

Simply add a folder 'raw' to your res folder.

Mandel
  • 2,968
  • 2
  • 22
  • 19
5

The R class is written when you build the project in gradle. You should add the raw folder, then build the project. After that, the R class will be able to identify R.raw.*.

Ian
  • 933
  • 12
  • 17
5

To get it to recognize the raw file, make sure you create it, and add it as a raw resource file. Then go to build.gradle file and add this block of code.

sourceSets {
    main {

        assets.srcDirs = ['/res/raw']

    }
}
Frank Fajardo
  • 7,034
  • 1
  • 29
  • 47
jrende
  • 51
  • 1
  • 4
5

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

Parkofadown
  • 616
  • 7
  • 11
4

(With reference to Android Studio)

Create a new directory in res folder. Make sure to create new "Android Resource Directory" and not new "Directory".

Then ensure that there is at least one valid file in it. It should show up now.

Sidharth Raja
  • 307
  • 1
  • 3
  • 10
1

Using IntelliJ Idea:

1) Invalidate Caches, and 2) right click on resources, New Resources directory, type = raw 3) build

note in step 2: I was concerned that simply adding a raw directory wouldn't be enough...

Coffee123
  • 156
  • 2
  • 9
1

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

android.resource://" + getPackageName() + "/" + R.raw.filename
Dharman
  • 30,962
  • 25
  • 85
  • 135
mehmoodnisar125
  • 1,469
  • 18
  • 14
1
  • 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.

0

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

NSR
  • 111
  • 1
  • 3
0

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)

Sarath Siva
  • 547
  • 3
  • 14
-2

Try putting a + before R.xyz. That should start showing additional folders, e.g.:

+ R.raw.beep.mp3
djikay
  • 10,450
  • 8
  • 41
  • 52
SHD
  • 101
  • 2
  • 5