raw folder in Android - java

I am trying to play sound effects in a application. Everywhere I look, I am being told to add a raw folder in the res directory(working in eclipse).
First, I add the raw folder under res. Then I put my .wav files inside. When I try to use R.raw.filename in my code, I get an error "raw cannot be resolved". So I clean and rebuild the project. Suddenly, everywhere that I used R.id or R.anything is underlined as an error. When I looked up the error, it turns out that it had something to do with Android.R. Nothing I did could fix it. So I removed the raw folder and rebuilt the project. All of a sudden, everything is fine.
I've repeated this process about 4 times and I have no idea why it's such a pain to add the raw folder. Can anyone tell me what I'm doing wrong?

Do any of your .wav files contains invalid characters in their name? Check if maybe the following messages are shown somewhere in your build:
Unparsed aapt error(s)! Check the console for output.
and
Invalid file name: must contain only [a-z0-9_.]
Only lowercase characters, digits, underscore and period are valid for the names plus extensions.

Use assets folder and ...
InputStream sound = getAssets().open("filename.mp3");
... or raw folder and ...
InputStream sound = getResources().openRawResource(R.raw.filename);
Just make sure that the file called filename exists in the directory called raw.
If all uses of R.* are underlined, delete the import android.R in the upper part of your class as Maxim mentioned.

Apparently your activity added import android.R, after that all references with R goes to android framework (android.jar) which doesn't know anything about your raw files. Just check imports and if you see android.R, delete it and add com.my_app_namespace.R Should work after that

I am not sure if this counts as a solution, but when I used .ogg files instead of .wav files, everything worked perfectly.

Related

While using ImageIO.read in java I get input == null

Here is my code: http://pastebin.com/CkD6485J
When I run it through I get an input == null! error and I marked all the places the error was caused by. I put the /res/ folder in the build path and it didnt work. I tried removing it, and putting it back in, and I tried removing the /res from the file location. I'm also following this from this tutorial: https://www.youtube.com/watch?v=WLhUzuTSTAc so I'm not sure why it won't work.
The resource /res/sheet.png should be in the same jar as the GameComponent.class` file. Also the path is case-sensitive. Open the jar with 7zip, WinZip or whatever and inspect the path.
It might be, that it should have been /sheet.png, when all below /res is added to the jar.
sorry about this but after a few days of of looking it over i figured out that there was something wrong with the .png file I used for my resource. I downloaded a completely new one and it works fine. Thanks for the ideas though

How to add images in res folder in android

I am trying to add some images to res folder in my android 4.2.2 application. The problem is the project runs fine until whenever I do add, upon running it gives an error
R cannot be resolved to a variable
in the src/MainActiviy.java and the R.java file deletes it self from the gen folder.
Now I'm naming the folder drawable-mdp and every png image is also lower cased. But it doesn't seem to bother.
Thanks!
You are getting this error because R.java failed to create. This happens when you try to a non-png image or the name of image contains some special character or characters in uppercase.
Ok guys I did try a few things and I finally got it. You can't use - hyphen while naming the image files themselves however _ underscore can be used.
Note you always have to use drawable- while naming the folder can't use _ underscore

raw folder cannot be resolved - android

I am using AndroidStudio, which is based on intelliJ Idea. I know that this issue was often presented here, but I created inside res folder some raw folder and copy-pasted some mp3 file jam.mp3. But I can't make my raw folder (inside my res one) resolved by my IDE and I tried numbers of tricks suggested in SO.
I have this line of code
MediaPlayer mp = MediaPlayer.create(this, R.raw.jam);
and raw cannot be resolved, how can I fix it ? (Btw, I can't find in my IDE an equivalent of the eclipse's clean)
In IntelliJ IDEA, you have a context menu option "Force generate R.java" when you stand on such a resource file.
Your song name should start with Capital Letter , Should not contain special characters like . _ etc..
If this not solves your problem then try restarting your IDE.

processing for android: AssetManager and list() not working

I've been developing for android using processing but have come to a halt when I wanted to retrieve a list of files within a given directory. Below is a screenshot of the code I have been trying.
I have tried different variations of this (such as getbaseContext().getAssets();) and nothing seems to work. Whenever the code tries to execute the list() part it has an error and there is nothing in 'fileNames'.
Am I missing anything? Is this a problem with processing?
Thanks
EDIT: The direction I am trying to access is "assets/Levels/" which I can see from my project view in eclipse.
I experienced a similar issue because I used folder names with a trailing / character.
So for others experiencing this issue, you need to make sure the folder name you use are just the folder name with no trailing / (so "movies" instead of "movies/")
You do not need any additional permissions in order to run the code that you have. I just compiled it on my device and it works fine. You mention you are attempting to list the files in a given directory. What you are doing is listing the files in the Levels folder, which should be a sub folder of assets. What you will get back is an array of all file names in that folder.
If you are actually looking to get a list of files from a given directory, it has nothing to do with the assets folder and I suggest you check out a tutorial on Android external and internal storage. The information directly on the android page was very helpful to me.
http://developer.android.com/guide/topics/data/data-storage.html
If that's not what you are looking for though, I suggest rewording your question.
So I got it to work!
Turns out it was a problem with where I was placing the 'String[] fileNames;' code before trying to put data into it. The below code works fine:
Thanks for the help!
*Note : Individual folder should not empty.. It will return zero for folder with no files. *
void getFolderDetais(String folder_name)
{
// pass folder_name empty to get detail of root that means assets folder details
String[] filelistInSubfolder = assetManager.list(folder_name);
Log.v("Floder Details ----- >", " Length --- "
+ filelistInSubfolder.length);
}

R file cannot see my src file

I am having trouble with my R file, I was able to get it running again but then i seem to have another issue. Nothing seems to be generating after all the work I put into it. I think my problem is coming from the fact that my src cannot see the xml files and I don't know why. My find_bluetooth_devices is underlined in red squiggles and says it cannot be resolved or is not a field. However, I know for sure that I have created a xml file that included everything for it to work. I feel as though the xml file is not being seen by the src as it should be. Does anyone know what I can do in order for the src file to see my xml file?
setContentView(R.layout.find_bluetooth_devices);
Make sure your import statement is for the correct R file. You could have mistakenly imported android.R instead of your projects R file.
Here, read through this and the next one and see if you can solve the problem:
Also, make sure you dont have import android.R at the top.
Where did my R file go?
Try cleaning the project
Project>Clean
There are two things that can cause that:
There's a syntax error in one of your XML files, this causes the R file to not be generated.
The R file is not included in your class, make sure you import com.yourproject.R in the class.
Clean your project
the project will rebuild and R.java generated
Change the compiler the project will rebuild and R.java generated
Create New Project and copy all java file and resources into new project

Categories

Resources