How I get the asset file path as String? - java

hi i want the path to a file in my android assets folder.
How I get the path as String?
I try with :
String path = getClass().getClassLoader().getResource("file:///android_asset//psp.pdf").getPath();
I need the path as String.

The only ways to access an asset are:
Via open() on AssetManager, in which case your path is psp.pdf
Via file:///android_asset/ in WebView, in which case your path is file:///android_asset/psp.pdf
An asset is neither a file on a filesystem nor something that you get from a ClassLoader.

Related

Why is android unable of finding my file?

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.

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)

How to set MS Access database path in UCanAccess?

This is my current database path
"jdbc:ucanaccess://D:/Java/TransactionProcessingSystem/src/transactionprocessingsystem/Resources/TPSystem.accdb"
But I want set the database path in the same compilation folder location, Like this:
"jdbc:ucanaccess://../Resources/TPSystem.accdb"
I am using Netbeans to develop the program.
It just must be a valid path(relative or absolute). Therefore, if you're using a relative path, it must be relative to the current working directory.
You have to use ClassLoader class.
Firstly put the resource file(TPSystem.accdb) in src/main/resource of your maven project.
ClassLoader classLoader = getClass().getClassLoader();
File file = new
File(classLoader.getResource("TPSystem.accdb").getFile());
and now the file Path will be
String filePath = "jdbc:ucanaccess://"+file.getAbsolutePath();

Java resolve project path

I am trying to return my project's file path to then create a file in said path. I successfully created the file in the path but I used the static path to my project rather than resolving it programatically like I need to do.
Using the docs i've tried creating my file:
Path path = Paths.get("C:\\folder\\folder\\folder\\folder\\folder\\report\\");
String filePath = path.toString() + "fileName.pdf";
createFile(filePath, data, moreData);
Question:
What if someone else uses a D: drive or other? How do I resolve the report folder if that is the case?
Use a relative path to the file, rather than an absolute path.
Path path = Paths.get("reports/fileName.pdf");
String filePath = path.toString() + "fileName.pdf";
createFile(filePath, data, moreData);

Opening a local file in Groovy

I want to use a File object to read a local file in the same directory as a groovlet. However, using a relative path to the file (either "example.txt" or "./example.txt") doesn't do the trick. If I give it an absolute path (e.g., "/example.txt"), then it works.
Is there any way to get the working directory or context path of the groovlet programmatically?
new File("${request.getContextPath()}/example.txt")

Categories

Resources