What is an asset in android? - java

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.

Related

Getting the full path to the assets folder

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")

Writing to text file created manually inside app

Is it possible in Android, to manually add a file inside a project and then, modify it? Example:
"I have a test.txt file in the following path: "app/src/data". I would like to make a method to write a given String in the test.txt file."
Is that possible? I been looking everywhere, but can't seen to do such an easy task.
If you mean modifying files inside the APK itself then it's not possible. Besides, the folder structure you see in the project is not the final structure on the APK (just unzip your APK, it's a .ZIP really): Fpr example, the source directory is all compiled into a classes.dex. The res/ directory is compiled and fully copied ...
Take a look at How to write files to assets folder or raw folder in android?
and https://developer.android.com/training/basics/data-storage/files.html
You can read raw files stored in /res/raw, or assets stored in assets/ , but you cannot modify stuff inside the APK itself.
What you can do is create and modify as many files as you wish from the different places Android gives to any app, such as:
CACHE directory (context.getCacheDir() -> /sdcard/Android/data/your.package/cache
External files (context.getExternalFilesDir() -> /sdcard/Android/data/your.package/files
Arbitrary directories in the SDCARD.

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?

Anatomy of a file, interaction with java File class

I was wondering what universal components there are to a file in a typical Windows x64 architecture and which elements of a file are stored in the OS vs the file... I know there is a file path, but does the file contain that path? Does the file keep track of its own length? Do simple .txt files contain more than the data that corresponds to the text they will display?
Further, when I create a File object in java, passing it an absolute file name, is it really just a reference to a file path? How does it interact with the actual file on my machine?

Where is the path "file:///android_asset/" documented?

There's a number of pages on the Internet talking about the "file:///android_asset/" path (sometimes with an s appended to the end...) for accessing the assets folder. I am unable to find any kind of official or trustworthy documentation on this path/feature.
Is there any proper documentation for this path? Did the Android team document it?
Android being open source its source code is the most accurate documentation. Here's from android.webkit.URLUtil:
// to refer to bar.png under your package's asset/foo/ directory, use
// "file:///android_asset/foo/bar.png".
static final String ASSET_BASE = "file:///android_asset/";
// to refer to bar.png under your package's res/drawable/ directory, use
// "file:///android_res/drawable/bar.png". Use "drawable" to refer to
// "drawable-hdpi" directory as well.
static final String RESOURCE_BASE = "file:///android_res/";
Strangely, I only found one hit for the following search:
"android_asset" site:android.com
That search result is for WebSettings, which mentions the android_asset and android_res paths in passing.
However, I'm sure I have read the "official" documentation for this before...
I found this:
http://developer.android.com/tools/projects/index.html
It provides the directory structure of a project.
The interesting part says:
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.
res/
Contains application resources, such as drawable files, layout files, and string values. See Application Resources for more information.
anim/
For XML files that are compiled into animation objects. See the Animation resource type.
color/
For XML files that describe colors. See the Color Values resource type.
drawable/
For bitmap files (PNG, JPEG, or GIF), 9-Patch image files, and XML files that describe Drawable shapes or Drawable objects that contain multiple states (normal, pressed, or focused). See the Drawable resource type.
layout/
XML files that are compiled into screen layouts (or part of a screen). See the Layout resource type.
menu/
For XML files that define application menus. See the Menus resource type.
raw/
For arbitrary raw asset files. Saving asset files here instead of in the assets/ directory only differs in the way that you access them. These files are processed by aapt and must be referenced from the application using a resource identifier in the R class. For example, this is a good place for media, such as MP3 or Ogg files.
values/
For XML files that are compiled into many kinds of resource. Unlike other resources in the res/ directory, resources written to XML files in this folder are not referenced by the file name. Instead, the XML element type controls how the resources is defined within them are placed into the R class.
xml/
For miscellaneous XML files that configure application components. For example, an XML file that defines a PreferenceScreen, AppWidgetProviderInfo, or Searchability Metadata. See Application Resources for more information about configuring these application components.

Categories