Why is android unable of finding my file? - java

I want to access a file in included in the folder of the app and I want to be able to access it localy. I also don't want to use methods like android.getresources() and just use
File file = new File( filePath );
My file is saved in app/src/main/assets/ball.obj
The problem is when I do File file = new File("app/src/main/assets/ball.obj");, it alwas gives me a FileNotFoundException. I also tried moving the file in other places and absolute pathes, but it still doesn't work. Please help.

My file is saved in app/src/main/assets/ball.obj
That is a relative path to a file on your development machine. Assets are not files on the filesystem of the device. You cannot use File to access an asset.
To get an InputStream on your asset, call getAssets() on your Activity or other Context, then call open() on the AssetManager returned by getAssets().
For your call to open(), you need to pass in the relative path within assets/ to the asset that you wish to read in. So, if you are in a method of your Activity, getAssets().open("ball.obj") will give you an InputStream that you can use to read in the contents of that particular asset.

Related

Where to save a file to be accessable via FileInputStream

I need a FileInputStream from a file but I don't know where to place the file.
I wrote this code:
InputStream is = new FileInputStream("src/test/resources/Key.p12");
But, where should I save file Key.p12?
InputStream is = new FileInputStream("src/test/resources/Key.p12");
There is no src/test/resources/ directory on any Android device.
Yes, but in app/src/test, there doesnt exist a dir called "resources". Only "res". Maybe it works when i create a dir called "resources"...
No, it will not. FileInputStream will only work for things that are files on the device. Anything packaged in your APK will not be files on the device.
Instead, store this file in app/src/main/assets/. Then, use AssetManager and open() to get an InputStream on that asset.
where ever you want to put the file
just copy the destination and paste it in the constructor

Cannot Construct An Image Object With Relative File Path WIthout Using file: Prefix

I have been trying to create an image object like this:
Image img = new Image("images/jack.png");
or
Image img = new Image("jack.png");
or /jack.png or /images/jack.pngetc.
I have looked up the working directory using System.getProperty("user.dir") and it is indeed where I put my image file. When I use file: prefix, it does work, like so:
Image img = new Image("file:images/jack.png");
However, it is also supposed to work without using it. In the textbook it is done without file:. I've seen other codes that work without it.
At the end of a bunch of chained exceptions, it says:
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
I also tried to read source code from OpenJDK and I could figure anything out because many methods were native and from what I traced I didn't understand how it didn't work. Also, I can create files the same way, I just can't create images. For instance, this works:
File file = new File("fileName.txt");
What causes this problem, what should I do to fix it?
I'm using NetBeans, if that matters.
Note that System.getProperty("user.dir") does not return the working directory. It returns the user directory.
A path relative to the working directory can be specified using a relative file path in the File constructor. However it's bad practice to rely on the working directory. Starting the application from NetBeans results in the working directory being the project directory, but this is not the case, If started in a different way.
Images you need in your application should therefore be added to the jar.
In this case you can retrieve the image URL via Class.getResource(). (convert to String using toExternalForm().)
If you have a File that references a image file, you can use the File instance to get a URL:
File file = ...
String urlString = file.toURI().toURL().toExternalForm();
Those URLs can be used with the Image constructor.
Note that
File file = new File("fileName.txt");
does not create a file. It just represents a file path. This file may or may not exist. Simply invoking the File constructor does not create a new one.
File file = new File("name.txt");
creates a file somewhere. It doesn't read the existing file whereas
Image image = new Image("pathToImage.png");
tries to read the existing image. In order to be able to read an image stored somewhere you need either the absolute path, which requires the protocol (http, file, ftp etc.) or you put your image into the 'known' directory, like the resources dir of your project.
Say, you have your java sources under src/main/java. The resources dir could be src/main/resources. Put your image there and try working with relative path relative to src/main/resources.

Get absolute path of a file [Android]

I have an app in which I write a file into the apps file folder(with a Fileoutputstream).
When i want to access it again I use a Bufferedreader with the location:
"data/data/com.mycomp.myapp.rstats/files/stats".
This works on pretty much all devices because the app gets installed there. But I imagine if somebody puts it on an external SD card, the path will be different. How can i get the absolute path of the file location?
You can use.. But do check for storage type exist before use it.
File extStore = Environment.getExternalStorageDirectory();

android - retrieve all files from /res/raw folder as File type

How can I retrieve all the files in /res/raw folder as File?
I have found this solution, and "New Folder" value I have replaced with "res/raw" as you can see in the following code:
public File[] getRawFiles() {
File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "res/raw");
return yourDir.listFiles();
}
When program is started, I get an exception on line return yourDir.listFiles:
Caused by: java.lang.NullPointerException: Attempt to get length of null array
How can I fix this, and what is correct path to "res/raw/" ?
How can I retrieve all the files in /res/raw folder as File?
You cannot do this. As we discussed yesterday, resources are not files on the filesystem of the device. They are files on your development machine. They are merely entries in an APK file on the device.
Either:
Do whatever you are trying to do some other way that does not involve files, or
Use openInputStream() on a Resources object (you can get one from any Context via getResources()), and use Java I/O to copy the contents of a resource to a local file, such as on internal storage, and using reflection to iterate over the actual resources, or
Switch to using assets/ instead of res/raw/, as AssetManager allows you to list() assets (though you still only get an InputStream on an asset, as like resources, assets are not files on the device)

Java - Audio URL

I have a audio file stored in a folder within the project and I'm trying to call the sound file within the folder.
I am able to call the file using the full URL
new java.net.URL("file:C:\NetBeansProjects\Puzzle\audio\applause2.wav"));
But is there a way to shorten it so that if I move the project I don't require changing the code?
Thanks
File file = new File("audio\applause2.wav");
Assuming you keep the audio folder at the same level relative to the jar

Categories

Resources