Getting the full path to the assets folder - java

I am using a method that require a full path for a file as a String, I have the data stored in the assets folder of my project and I cannot seem to get to it via file:///android-assets/my-file, I think it because the method wants a FULL path. is there a solution to this or should I try a different storage option?

For URI
Its file:///android_assets/{Relative Path}. Note the 'underscore' between the words android and assets, not 'hyphen'.
However there's no absolute path for asset files, but you can get an Input Stream of the file like this
getResources().getAssets().open("fileName")

Related

Full path to files in res/raw folder

I have an Android app (targeting Android R - API lvl 30) which has bulk of its logic in c++.
App reads a file in res folder and we can access it like - R.raw.dev, where dev is the config file.
My intention is to get the full path to this file and open it in C++ layer using fopen method. But I'm not able to get the full path to the file.
I have seen some suggestions of the following kind,
Uri resRawUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + getPackageName() + R.raw.dev);
But this is an Uri....not a file path.
I've heard of an alternative to the res folder, called assets, which gives more freedom than the res folder. The methods of AssetManager, the class used to query the assets folder, return InputStream and others, but not the file path.
Is there a way to get the absolute file path to files in res or assets folder? If its not possible in these two folders, how about external storage? Is there a way to get the absolute file path, so that I can pass this path to C++ and open it using fopen?
My intention is to get the full path to this file
Resources are not files on the device. They are files on your development machine. They are entries in an APK file on the device.
Is there a way to get the absolute file path to files in res or assets folder?
No, sorry.
If its not possible in these two folders, how about external storage?
I would use internal storage, such as getFilesDir() or getCacheDir() on Context.

Need File or URL, referencing a folder (NOT FILE) from raw or assets

I have once again had trouble using JWI Wordnet in Android. The only way to access it, is to create a URL or File pointing to the directory where the files it needs are located. http://projects.csail.mit.edu/jwi/api/index.html I have downloaded the files I need, and added the jar into my project. My error is when I try to create the Dictionary object used to access wordnet. I don't point to a valid directory. Which makes sense, but I just don't know how to do so pointing the correct way. Basically, the folder name is "dict", I can place it into either raw or Assets (Although I read something about files in assets needing to be < 1 mb, the files inside the folder are greater than 1 mb) I need to create either a File or URL that points to said folder. How would I go about doing this?
So, you cannot create a file from raw or assets directly, as they are not stored as files, but you can copy/unpack them somewhere else, and make a File object using the copy. Android: how do I create File object from asset file?

Get assets directory from an android function name or variable instead of hard-coded

Read many questions about the location of assets. Not any of these solutions uses a function to get the assets foldername/dirname/pathname, only hard-coded like: "file:///android_asset/".
For example to get a path you can do something like this:
String sFilesPath = mContext.getFilesDir().getPath();
Is there a function or variable in Android to get the full path to the assets folder?
No there is no way to get Absolute path for your assets folder as they come with your apk. If you need to access the assets folder you need to use file:///android_asset only. There is no other way.

What is an asset in android?

I'm trying to load words into an arrayList from a txt file in the assets folder in my android program, and looking at the api, it says that AssetManager.list(String path) will "Return a String array of all the assets at the given path."
Does that mean that if I have a text file, words.txt, and I put in "words.txt" as the parameter for AssetManager.list(String), that it will return a String array of all the words in the txt file?
If not, how would I go about reading a txt file into an array in my android program?
I couldn't find a definition for "asset"
I'm using eclipse.
EDIT:
I'm not just asking what an asset is, I'm also asking about a specific method in the api and how to load strings into an arrayList
Android offers one more directory where you can keep files which also will be included in package. This directory called /assets. The difference between /res and /assets is that Android doesn’t generate IDs for assets content. You need to specify relative path and name for files inside /assets.
assets/
This is empty. You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.
Source
Asset Usage Example
assets is place where you can normal files which you want to read in application as some point whenever needed. You save your files/directory's. And read in your application from input streams but you cannot write.
As far res concerned, they resource files which you can refer directly in your code. like strings, array of strings,images (drawables),style(android specific like css in html) and there is raw folder which stores all files binary and generates id for it.

Creating Directories to categorize the images based on its type in Java/Android?

I have to create a multiple directory like this path("images/wallpaper/flower"). It should be stored in the External Memory of the phone.
How to do it? Any Idea?
Just get the SDcard path using Environment.getExternalStorageDirectory(); and then create the dir structure with the Java's File class.
You should know that the sdcard can be unmounted so check it's status first.

Categories