I just started a new Java Project and I'm working on Animations atm, my question now is how do I link my res source folder with the project so that I can read images via
img = ImageIO.read(getClass().getClassLoader().getResourceAsStream(name));
from that folder? Normally I they can only be read when they are in the
if we assume that your resource folder is called "resources":
Image image = Toolkit.getDefaultToolkit().createImage(yourclassName.class.getResource("/resources/..."));
Related
I have a file with some data that I want only my application to have access to.
I was wondering in which folder I should put the file in my android project in order to have access to it using
File file = new File(context.getFilesDir(), filename);
Thanks
This is just an empty folder in Android device.
To access your files in assets
InputStream is = getAssets().open("subfolder/anyfile.txt");
to access files inside raw use
InputStream XmlFileInputStream = getResources().openRawResource(R.raw.somesrc);
getFilesDir() is just a folder that uses for openFileOutput(String, int) method. More detailed: https://stackoverflow.com/a/21230946/1979882
Use Assets Folder For files that will be bundled with the application and will be stored inside the apk as is. you can add an "asset" folder to your project by right clicking your app in the project explorer then select
New=> Folder=> Assets Folder.
you can open the files stored in the assets folder using the assets manager for example to open an image kept in /assets/img/image1.png:
InputStream is = assetManager.open("img/image1.png");
for HTML files you can load the file by its URL :
webView.loadUrl("file:///android_asset/"+name);
Here you can get a clear functionality of saving,retrieving a file
Here is the Link:http://www.mysamplecode.com/2012/06/android-internal-external-storage.html
How to crate a source folder with images and then use them in that same project?
I have an "image" folder in src and I indicated that it is a source folder via build-path. I tried like a million ways to put those images on my JFrame-JLabels but none of them seems to work(it does with absolute path).What is the best way to do that?Relative path needed...
Make a resource folder. Call it "res" or whatever you want. Or you can make a method which sets directory already to folder you want and you just input file name
img = imageName.png
buffImg = ImageIO.read(new File("C:\Project\src\images\" + img));
i have created a plugin which can load the image from the icons folder as follow:
Image image = ImageDescriptor.createFromURL(
FileLocator.find(bundle, new Path("icons/image.gif"), null))
.createImage();
It worked fine.
Similarly I have created a folder called "assets" inside eclipse plugin project, & created a file called "Temp.txt" inside it.
But I am unable to find a way to load it into my java class. Kindly help me out in getting through this.
Use:
URL url = FileLocator.find(bundle, new Path("assets/Temp.txt"), null);
URL fileUrl = FileLocator.toFileURL(url);
File file = new File(fileUrl.getPath());
... read the file in the usual way
I created a java app and I put some images in it and even gave it an image icon as the desktop image, but when i made it a jar file, and put it on another pc, all images were gone. This is the image path :
File imageFile = new File("C:\\Users\\Favour's Computer\\workspace\\Physics Calculator\\src\\res\\icon.jpg");
I checked it online and i found out that the problem is that i got the file through the C:\\ directory, they said the image file should look like this :
File imageFile = new File("res/icon.jpg");
I tried this but it didnt work, I kept getting an error message like : file not found
This is my full code :
BufferedImage image = null;
try {
File imageFile = new File("C:\\Users\\Favour's Computer\\workspace\\Physics Calculator\\src\\res\\icon.jpg");
image = ImageIO.read(imageFile);
} catch(IOException e) {
e.printStackTrace();
}
setIconImage(image);
Please, i have been trying to solve this for weeks, do anyone know how i can solve this, please if you do, please help
The images should not be loaded from the file system, but should be bundled into the app, inside your jar.
If you put the image foo.png inside the jar, under the package com.bar.resources for example, you simply need to use
InputStream in = getClass().getResourceAsStream("/com/bar/resources/foo.png")
to load the image as an input stream.
That will use the class loader to load the image. So, during development, if you're using a standard IDE project, you just need to put the image file in the appropriate package in your source directory: the IDE will "compile" the file by copying it to the same directory as the generated .class files.
If you're using a standard Maven/Gradle project, then it needs to be put in the appropriate package under src/main/resources.
Problem is you dont have res folder in your project, first of create a folder a "res" by Right Clicking on project and place the image icon.jpg image in that "res". and
use : `File imageFile = new File("res/icon.jpg");`// To retrieve image to your project.
You cannot retrieve the image from your local system, because it is not attached with your projects workspace.
There are two options....
1)Either make res a "source folder" . You will know this if you are using Eclipse.
Then you can use like
ImageIO.read(new File("res/icon.jpg"));
2) If res is a normal folder, you will have to use. In this case res will be considered as a package
ImageIO.read(new File("src/res/icon.jpg"));
I am developing a little game where I use some pictures for the sprites etc etc.
It works just fine when I load it from the disc like this
Image image = new Image("C:\\AppleMan.png");
but how can I load it from a foloder within the project. I am using eclipse as IDE and the language is Java :)
I took a screenshot of a sample project so you can see how I have importet the picture
So I want to load the picture from that resource folder like this pseudo code
Image image = getResource("Resources/AppleMan.png");
but that simply doesn't work.
Any help appreciated :)
1) You should add Resources folder to classpath
2) You should locate file absolutely, i.e. "/Resources/AppleMan.png"
P.S.
3) Sorry, also note that getResource returns URL: http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getResource%28java.lang.String
You're passing a relative path when you were passing a absolute path before. Add the path of the Eclipse workspace.
For example, if your workspace is at C:\Workspace, you need to put
Image image = getResource("C:\\Workspace\\HowToGetResources\\Resources\\AppleMan.png");