I have created a android project in Visual Studio.I have a few images in the the Drawable folder. These images show in the Resource designer as well. But when I try to access these images the Project Resource explorer does not show them. Also if I try to use them programatically they don't show up.
What is wrong?
to get resource from drawable folder use this line
getResources().getDrawable(R.drawable.your_resource_item);
Make sure that the "Build Action" on the specific file is "AndroidResource".
Find the resource file in the file explorer, right-click on it, and select the "Build Action" menu item.
It is all very well to have the file in the right file directory location in the operating system, but the Android compiler needs to know what to do with it as well.
Related
I need to make a game for school.
I was adding some sound effects and music.
I have the paths to the .wav in the class and it will play them, but only when I start the game from the bin map in the project. I figgered out that the files are not included inside off the build. I started looking for it but could not find the correct way for it.
I have:
MediaPlayer _musicPlayer = new MediaPlayer();
string s = System.IO.Packaging.PackUriHelper.UriSchemePack;
Uri uri = new Uri(#"pack://application:,,,/{Assembly name};component/Resources/Sounds/Music/Main_theme_-_Thiago_Adamo.wav")
_musicPlayer.Open(uri);
_musicPlayer.Play();
This however does not work. If I add an messagebox with:
uri.IsFile.ToString()
It displays false.
On the file I set the propeties to be:
Build Action: Resource
Copy To Output Directory: Copy Always
I hope someone can help me.
Add the files to the project in your solution in Visual Studio, so they show up in the Solution Explorer (docked on the right side of the VS window by default). Click on a file so you see the properties window for that file (shown below the Solution Explorer in VS by default). Make sure the Copy to Output Directory property is set to either Copy Always or Copy if newer, and the Build Action is set to Content. Do this for each sound file you need.
At this point, the files will be copied to same folder as your application when you build it. They will not be packaged. They'll just be there in the file system next to your program. If you build the application in Debug mode, this is in the /bin/Debug folder (by default) for your project. If it's Release mode, it's the /bin/Release folder, but as long as your working directory matches your application you can reference them in the Uri by file name alone (otherwise you'll need to check the application path and build the full path to the file).
The reason you need Content instead of Resource is in the Remarks section of the MediaPlayer class documentation:
When distributing media with your application, you cannot use a media file as a project resource. In your project file, you must instead set the media type to Content and set CopyToOutputDirectory to PreserveNewest or Always.
I added a folder to my project by right clicking on the project and adding a new folder. Now I added the image to the folder (using copy paste in Windows File Explorer), but the solution explorer is not showing my added image. I did refresh the solution as well.
Also, in that folder, there is no option of adding an image, only Visual Studio files (new items etc)..
Why isn't Solution Explorer showing my image?
You just need to have an existing file, open the context menu on your folder , and then choose Add => Existing item...
If you have the file already placed within your project structure, but it is not yet included, you can do so by making them visible in the solution explorer
and then include them via the file context menu
You need to turn on Show All Files option on solution pane toolbar and include this file manually.
Click on the Project in Visual Studio and then click on the button titled "Show all files" on the Solution Explorer toolbar. That will show files that aren't in the project. Now you'll see that image, right click in it, and select "Include in project" and that will add the image to the project!
If you're having an issue where the Resources added are images and are not getting copied to your build folder on compiling. You need to change the "Build Action" to None from Resource ( which is the default) and change the Copy to "If Newer" or "Always" as shown below :
I'm having problem loading images to my project.
I have the image in a folder named microassig this folder is on my desktop, this folder is to be sent online to my tutor, thats my line of code:
private Image imageOpen = Image.FromFile("\microassig\openOff.bmp");
I don't want to put the directory c:/ because is directory will be different from mine hence why i'm just using the ("\microassig\openOff.bmp");
The problem is that the image doesn't load.
Locate your project's
bin\debug folder
You can do this by right-clicking on your project solution and clicking Open in Windows Explorer.
Store/Save the file as
openOff.bmp
Then you can just do:
private Image imageOpen = Image.FromFile("openOff.bmp");
You should not be using the Image.FromFile. You need to add the image as a resource into the project. See this link: http://msdn.microsoft.com/en-us/library/7k989cfy(v=vs.80).aspx
The \ doesn't imply your desktop. You should use GetFolderPath for that (See C# Get Special Folder)
There are a few ways to go about this. You could add your image as a resource.
Or, more in the line of what you are already doing you could change your code to something like this:
private Image imageOpen = Image.FromFile("openOff.bmp");
Then move the openOff.bmp to your bin folder (where the exe is saved).
While you keep the openOff.bmp in the same folder as your executable it should find it.
I am making a console program in VS2010. The structure of as layed out in the solution explorer is Project file, Properties folder, References, and Program.cs.
I want to make use of a text file for reading/writing. What is the correct way of adding this file to the project? If I just do StreamWriter sw = new StreamWriter("maze.txt") then it'll create the file in the output folders (bin/debug or bin/release). But it won't show up in the solution explorer.
Now if I right-click on the solution explorer and Add New Item, I can create a text file in the root project folder (same level as Program.cs). This will show up in the solution explorer.
Is there a way for me to access this newly created file? Other than doing something like StreamWriter sw = new StreamWriter("../../maze.txt") by specifying the path to be two parents up?
How am I supposed to manage external files in a Visual Studio console applicatoin? In a WinForms application, there's a resources folder where I can add these things and a Resources.resx file to manage it. I can access it with Properties.Resources.someres.
If you want to create a file at design time and have it included in your bin folder then add the text file, go to properties, and select "copy always" or "copy if newer" for the copy property.
Note that this will copy the file from the project to the bin folder, but changes in the bin folder when debugging won't be copied up into the project itself. I doubt you want the program to interact with the version of the file in the project itself. If you do something like that then anytime you run the program outside of VS (i.e. when you copy it to the machine of an actual user) it won't work.
As for your resources question, you can still use resources. Just right-click and "Add" -> "New Item" (aka keyboard shortcut Ctrl+Shift+A) and choose "Resources File". You could also set the text file to be an Embedded Resource. MSDN has a lot of information on using resources on their Managing Application Resources page.
You can still embed resources to a console application. I've used this technique in quick apps so we don't need a installer or deploy dependencies such as these type of files. This question discusses a similar topic.
What's the way to add custom images/icons in WinForms application.
I added a folder (right-click project and add new folder) named it then as my images folder and I would like to use this folder as my main images folder.
I don't see any option to use my 'images' folder in Visual Studio after dropping the Picture Box control.
Add the image as an embedded resource. You can the set the PictureBox's Image property to the resource via the property editor or at runtime by accessing the Properties.Resources object. The images will be compiled directly into your executable, you just need to add them to your project.