This question already has answers here:
Difference between /res and /assets directories
(8 answers)
Closed 9 years ago.
in this tutorial for "learning to parse XML data in your Android App" the author puts his XML file into the assets folder. But in other tutorials they recommend to use res/xml.
None of them explains why they took the folder they use.
What is the difference between those two folders? What is best practice? Is there a difference in performance?
The res folder is used to put authorized resources files type, like layout(xml), drawable (png, jpg, xml), raw (mp3, ogg), etc... In the assets folder, you can put all files types you want (txt, avi, png, docx, xyz, ext, asm, ....).
Check this : Android Projects Structure
1) 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.
2) 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.
The difference between "resources" and "assets" isn't much on the surface, but in general, you'll use resources to store your external content much more often than you'll use assets. The real difference is that anything placed in the resources directory will be easily accessible from your application from the R class, which is compiled by Android. Whereas, anything placed in the assets directory will maintain its raw file format and, in order to read it, you must use the AssetManager to read the file as a stream of bytes. So keeping files and data in resources (res/) makes them easily accessible.
For details you can see following links :
Link1,
Link2
Related
I have some problems while loading the file from the asset folder.
I wrote a code to load a txt file into a textview and everything works good.
If I try to put some order inside my txt file. I decided to put those files in subfolders in alphabetic order, because I have 800 text files. In this way my code is wrong. Is there a method to maintain the subfolders and load the file whitout specify where they are??
Thanks a lot
in Delphi, I add image resources to the project (via project > resources and Images), however i need for one java function to give a resource ID in the application's package of the drawable to use. Is it possible to retrieve a resource ID (integer) from my delphi resource file or name ?
If not, how in delphi we can add a custom resource image and retrieve it's resource ID ?
Getting a Resource ID
To retrieve the resource ID of a resource:
id := TAndroidHelper.GetResourceID('my_image', 'drawable');
If you are using an older version of Delphi you may need to use the alternate, older approach:
id := TAndroidHelper.Context.getResources.getIdentifier(StringToJString('my_image'), StringToJString('drawable'), TAndroidHelper.Context.getPackageName);
The shorter, more recent (and convenient) GetResourceID function is merely a wrapper around the earlier longer winded version.
In either case, my_image is the filename (without extension) of the image resource whose ID you need.
The drawable parameter tells the helper that it is a drawable resource ID that you are asking for (as opposed to a string or layout, for example).
Adding Image Resources
To add a custom image resource to your project use the Deployment option of the Project menu in the IDE.
Add your file and set the remote path to a suitable drawable resource folder, e.g.:
res\drawable
res\drawable-xxhdpi
etc
You could put your own scaled versions of the image in each drawable folder for different resolutions. The remote_name must be the same in each case. The specific drawable folder determines the applicable device resolution.
Alternatively you could simply provide one suitably high resolution file. You can place this in either an appropriate high-resolution folder (e.g. drawable-xxxhdpi) or simply in drawable.
Either way, Android will auto-scale the images at runtime for other device resolutions as necessary.
There are lots of Android references on the subject of drawable scaling, alternative versions for different display types etc, including the Android documentation itself of course.
Referencing your comment, there are however no XML files required for adding drawables that are straightforward images.
Additional XML may be necessary for other types of drawable resources, but that will very much depend on the nature of your specific needs.
Bringing It All Together
In the screenshot below the highlighted entry is one that I have added for a PNG image resource. The file is in my project folder so there is no local path (the first column).
The filename is appstore.png and I have configured the deployment to place one copy of that file in the res\drawable' folder of the application when it is deployed (theremote` folder).
This file will rely on auto-scaling for display on different resolutions devices.
To get the resource ID of that resource I then simply write:
id := TAndroidHelper.GetResourceID('appstore', 'drawable');
I'm going to develop an application which shows a ListView which each row have a seperate image (like personal picture for contacts list). It seems I have two options:
1- Store all images in Asset Folder and load image using setImageDrawable() command.
2- Store all images in Drawable Folder and load theme using setImageResource(R.drawable.xxx)
So my questions is does they differ in performance? And how can I speed up listView rendering such that images displayed in acceptable speed.
There should not much performance different to access image from Drawable or asset folder. You can see the answer too - > Android: Accessing images from assets/drawable folders
But, When you using ListView you are recreating the view . So, Where ever you store the image all the images are will not feel much different .
There are not so much differences. Just for coding pattern changes.
assets/
Bitmap files (.png, .9.png, .jpg, .gif) or XML files that are compiled into the following drawable resource.
drawable/
Arbitrary files to save in their raw form. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.
https://developer.android.com/guide/topics/resources/providing-resources.html#ResourceTypes
Is it possible to create our own folder in drawable and put all my images in that folder as:
"drawable\Myimages\p.png.."
If it possible than how can i use this images in my activity..gnerally we use "R.drawablw.p" but now how that can be written because now my images is in drawable\Myimages directory
please suggest me..
with regards
Anshuman
No, you can't create your own file hierarchy in drawable directory.
But you can see in http://developer.android.com/guide/topics/resources/providing-resources.html:
However, if you need access to original file names and file hierarchy,
you might consider saving some resources in the assets/ directory
(instead of res/raw/). Files in assets/ are not given a resource ID,
so you can read them only using AssetManager.
If you have very many resources, it can be useful for you.
No, the resources mechanism doesn't support subfolders in the drawable directory.You can't used.
First I thought that I could find this list on the net, but I'm either looking for the wrong term or such list doesn't exist.
What I need is basically a cheat sheet of all predefined resource folders in an Android project. For example, a list could say something like this
res/drawable - all graphics go here
res/drawable-hdpi - all graphics of higher resolution go here
res/layout - some-meaningful-description
res/values - some-meaningful-description
res/layout-land - some-meaningful-description
etc.
I am really surprised that such list isn't easily found on the net. Whenever I need to add some resource I haven't used before, I have to look on the net for the correct naming (and I would rather look at the list of res folders).
Taken from here:
In the /res folder you can have:
animator/ -XML files that define property animations.
anim/ - XML files that define tween
animations
color/ - XML files that define a state list of colors.
drawable/ - Bitmap files / Nine-Patches (re-sizable bitmaps)
/ State lists / Shapes / Animation drawables / Other drawables
layout/ - XML files that define a user interface layout.
menu/ - XML files that define application menus, such as an Options
Menu, Context Menu, or Sub Menu.
raw/ - Arbitrary files to save in their raw form.
values/ - XML files that contain simple values, such as
strings, integers, and colors.
arrays.xml for resource arrays (typed arrays).
colors.xml for color values
dimens.xml for dimension values.
strings.xml for string values.
styles.xml for styles.
xml/ - Arbitrary XML files
Also see Accessing Alternative Resources for more specific device configurations (locale, dpi, size, aspect, orientation, etc)
Never thought about it, but you are right.
You can always create a new project based on available samples projects in eclipse and see they have chosen to organize it.
You can see how one of my project is organized.
my project in eclipse
It should be noted that these folder layouts are not written in stone. One can create a folder tree in whatever manner one chooses. This is just the default tree created with IDEs such as Eclipse and is, as a result, the most popular and often consistent format. If you find another format to fit you better then you are free to use it. Just reference properly in your code.