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

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.

Related

How to include raw files without Android Studio/XML?

I'm working with Sublime Text & a command line ANT build script. How do I include data files that I can access and read through code?
I tried using getResources().openRawResource(); but the parameter is an int, not a string.
Also, I do not have the R class because it gets generated automatically. I also don't have any XML files besides AndroidManifest.
How to include raw files without Android Studio/XML?
The same ways that you do with Android Studio. Either:
Add them as raw resources (e.g., to res/raw/) and access them via getResources().openRawResource(), or
Add them as assets (to an assets/ directory, the location of which depends on your Ant(?!?) script) and access them via getAssets().open()
I tried using getResources().openRawResource(); but the parameter is an int, not a string.
Correct. That is a resource ID. For a raw resource, it will be R.raw.something, where something is the base name of the resource file. So, for a res/raw/foo.mp3 file, you would use R.raw.foo. This is no different than layout resources (R.layout), drawable resources (R.drawable), and every other type of resource.

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.

Concept behind the Java resources, striking gs, "R.id"

I ran into an issue that made me realize there is core concept I'm not getting.
For some reason when I went to add a value to my strings.xml file, something happened and my xml file was giving a parsing error. The only way I was able to fix this was by deleting the strings file and re-creating it by right clicking on the values folder, and going to "add android xml file" and naming it strings.xml.
This made the error go away, but now, my activities are complaining and saying they can't resolve R (as in R.id).
This made curious as to what's the relationship between R and the xml files. And why is that when you reference a string, you use #String/something, when the file that contains it is called strings?
Finally why is it that even though I created an xml file called strings.xml, in the right location, my project doesn't see his as the THE strings.xml file?
Names
The R resources are organized in a specific manner:
R.layout.* - These resources are your layout files, which you have created in the various layout-* folders
R.menu.* - These are the resources which are your menu files, created in your various menu-* folders.
R.id.* - These are the resource identifiers for your various Views and Menu items etc. There can be more than one element with the same ID, as long as they're in separate files.
R.string.* - These are your string resources, declared in <string> elements in files in your values-* folders. The filename is irrelevant. There can only be one string element with a particular name in a single folder. So you cannot have two hello strings in values, but you can have one each in values and values-en.
R.drawable.* - These are the various XML and image drawables you have in your drawable-* folders.
There are many more, but these are the most common. I'll add the rest when I have a bit more time on my hands.
After compilation
During compilation, the Android build system puts all XML files into a binary format. Everything except the /res/raw is compressed as well. This is particularly helpful with layouts, which can be inflated faster after compression. Individual files cease to exist after compilation, and everything becomes one big binary file. You access the data in this file using R.<resource_type>.<name> from Java, and #<resource_type>/<name> from XML
Naming
For some resources (drawables, layouts, menus) the filename matters, as the resource is referred to using that. For other resources (strings, colors, styles), the filename is irrelevant, as long as you use the correct element (for example <string> for Strings) when declaring them within the file.
When your project gets compiled and built, one of the things that happens is the generation of the R file. Each named resource is given a public static final int identifier, like R.string.foo, which would reference a string defined with name="foo". This way you can reference a resource from Java and Android will map it to a resource and load it from your application package.
Resources can be overloaded; you could have two strings both named foo that are qualified for different configurations, but they have the same identifier in the R class. The system will choose the one that best matches the device configuration at run time.
If there is an error with any of your resource declarations, R will fail to build and any java classes that reference it will therefore complain about it. You can do Project > Clean in Eclipse and see if that helps, but otherwise make sure your resources are properly declared and/or have valid names.
R.java is a seperate file created by eclipse,
its not related to strings.xml ,Clean your project then build and run again .
you will found R.java inside your gen folder
Read this
http://developer.android.com/tools/projects/index.html

How to attach an audio file into library?

I am developing an application with Android/Java. I need to attach a list of audio files into my java library. So that the developer can use the library and play the audio files. How can I attach the audio files into my Java library and how to call them as file path to play them?
If you are creating an Android Library, you can put your audio file in the raw folder. People using your library can refer to it using com.example.yourlibrary.R.raw.sample_audio_file.
I hope this is what you were looking for. The file will be embedded when the library is created.
If you have specific audioFiles then add them to the special resource folder and refer to them as getResourceAsStream . If you want user to use their own files - provide the variable for the folder path and make a filter to use only specific files, i.e *.mp3.
If they are stored in specific resources folder, why not to create special Accessor class for the developers to use it? Like you will have a *.jar file that will contain files in the separate resource folder with the Accessor to work with them.

A static resource file?

Android uses a static resource file R. This file (at least in eclipse) is automatically updated when ever you add new id's of any sort. How can I create/implement the same feature in a normal java application? Is it as simple as just writing an xml parser and just updating the resource file after the xml is modified?
In a way, yes. You need to create a custom build script/program which runs at the start of each build (before anything else), scans your resource folder files (and if they are XML files it needs to read in the XML files and parse out the string resources or whatever from those), then write it all to a Java file in some manner (e.g. R.string_name = "string value").
Make sure the XML files aren't actually packaged in your .jar, since all that information will be stored inside your Java resources file now.
For things which aren't XML files you could just store the filename as a string in the Java resources file.
You didn't specified the type or the use of the resources. I don't know android, but I'll try to help; If you just need to access some resource in your application you can use properties or resource, there are some differences see this other question Properties vs Resource Bundle

Categories