How to add images in res folder in android - java

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

Related

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.

JAR won't work after adding images

I've written a game that uses PNGs, and when I exported it, the images wouldn't work. I added getClass().getClassLoader().getResource() to everywhere I import an image, but the JAR won't even launch anymore. It used to open, but none of the images worked. Now it won't even open the JAR.
This is how I get the image for the muffin:
muffin=new ImageIcon(getClass().getClassLoader().getResource("muffin.png")).getImage();
Actual path for it is: C:\Users\My User Name\Dropbox\FinalProjectWithoutApplet\muffin.png
What should I do to solve this issue?
Thanks!
getClass().getClassLoader().getResource("muffin.png") is looking for the image muffin.png in the same directory where your class is.
I means that if your class's name is com.mycompany.game.MyClass the image is expected to be in com/mycompany/game/muffin.png. If this is not the location write absolute path that starts with /, for example /img/muffin.png.
And in future if program does not work start from examining the stack trace.
I found the issue. Turns out my cases mismatched. The IDE didn't care that one was called "muffin.png" in code vs. "Muffin.png". Another case I found was "DoubleJumpIcon.png" vs. "DoubleJumpIcon.PNG". Every single case matters it turns out.

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);
}

How to use JavaCv's cvSaveImage in android

I have created an app using javaCv and
I'm trying to save an image in android using
cvSaveImage("/storage/sdcard0/watermarked/test.jpg", yCrCb);
cvSaveImage("/storage/extSdCard/test.jpg",yCrCb);
where yCrCb is an IplImage.
There is no exception error and the program runs smoothly but the files are not saved into the path as mentioned above.
I would like to ask what might be the possible problems ? is it the naming convention of the file name ?
If it helps, I have a java application counterpart of this app and the java version works fine when i use the line
cvSaveImage("C:\\testing123.jpg", yCrCb);
Hi I have used same thing for storing the image.
For getting the path to sdcard, I have used the Environment.getExternalStorageDirectory().toString();
append the folder name to this path where you want to store the image
try this and also make sure that you have written permission in the xml file.

raw folder in Android

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.

Categories

Resources