When Should I Use the Assets as Opposed to Raw Resources in Android

When should I use the assets as opposed to raw resources in Android?

The main differences between the raw folder and the assets folder.

  • Since raw is a subfolder of Resources (res), Android will
    automatically generate an ID for any file located inside it. This
    ID is then stored in the R class that will act as a reference to
    a file, meaning it can be easily accessed from other Android classes
    and methods and even in Android XML files. Using the automatically
    generated ID is the fastest way to have access to a file in Android.

  • The assets folder is an “appendix” directory. The R class does
    not generate IDs
    for the files placed there, which is less compatible
    with some Android classes and methods. File access in the assets folder is slower since you will need to get a handle to it
    based on a String
    . However some operations are more easily done by
    placing files in this folder, like copying a database file to the
    system’s memory. There’s no (easy) way to create an Android XML
    reference to files inside the Assets folder.

Should an extra file be placed in raw or assets folder?

assets/ is flexible in terms of structure. You can have your own directory tree in there, for organizing lots of files.

res/raw/ is flexible in terms of resource sets. You can have different versions of that resource for different configurations (e.g., res/raw/ as the default, res/raw-zh/ for use on devices configured for Chinese language use).

If you do not need any of those features, assets/ and res/raw/ are about equal in terms of capability. I tend to default to assets/.

Difference between /res and /assets directories

With resources, there's built-in support for providing alternatives for different languages, OS versions, screen orientations, etc., as described here. None of that is available with assets. Also, many parts of the API support the use of resource identifiers. Finally, the names of the resources are turned into constant field names that are checked at compile time, so there's less of an opportunity for mismatches between the code and the resources themselves. None of that applies to assets.

So why have an assets folder at all? If you want to compute the asset you want to use at run time, it's pretty easy. With resources, you would have to declare a list of all the resource IDs that might be used and compute an index into the the list. (This is kind of awkward and introduces opportunities for error if the set of resources changes in the development cycle.) (EDIT: you can retrieve a resource ID by name using getIdentifier, but this loses the benefits of compile-time checking.) Assets can also be organized into a folder hierarchy, which is not supported by resources. It's a different way of managing data. Although resources cover most of the cases, assets have their occasional use.

One other difference: resources defined in a library project are automatically imported to application projects that depend on the library. For assets, that doesn't happen; asset files must be present in the assets directory of the application project(s). [EDIT: With Android's new Gradle-based build system (used with Android Studio), this is no longer true. Asset directories for library projects are packaged into the .aar files, so assets defined in library projects are merged into application projects (so they do not have to be present in the application's /assets directory if they are in a referenced library).]

EDIT: Yet another difference arises if you want to package a custom font with your app. There are API calls to create a Typeface from a font file stored in the file system or in your app's assets/ directory. But there is no API to create a Typeface from a font file stored in the res/ directory (or from an InputStream, which would allow use of the res/ directory). [NOTE: With Android O (now available in alpha preview) you will be able to include custom fonts as resources. See the description here of this long-overdue feature. However, as long as your minimum API level is 25 or less, you'll have to stick with packaging custom fonts as assets rather than as resources.]

Difference between res/ and resources/

The test folder contains code (usually test code, unit tests, etc...) that runs on your local PC or laptop. The code compiled and eventually run using the installed Java on your machine the same way as pure Java projects with some minor differences. So basically the purpose of the test folder is to test parts of application code that don't have a dependency on the Android SDK and without using an Android device. Also on the side, there's another test folder which is androidTest, it also used for testing parts of the application but with using an Android device, meaning an actual test application is loaded on the device and it executes the tests which have a dependency on the Android SDK.

That being said, pure Java projects or code usually use resources folder and not res (as far I have seen while using IDEs). When compiling and then running the pure Java projects or code, the contents of the resources folder are placed at the root of the Java classpath so that the code being executed finds its resources there.

Android Studio or the Android Gradle Plugin seems to handle both differently (in terms of packaging them into the final aar or apk), so it is best that there is only res in main or androidTest folder and also that there is only resources in test folder.

Reading assets or raw or resource files as a File object in Android

Here is what I did:

Copy Your asset file into SDCard:

AssetManager assetManager = context.getResources().getAssets();

String[] files = null;

try {
files = assetManager.list("ringtone"); //ringtone is folder name
} catch (Exception e) {
Log.e(LOG_TAG, "ERROR: " + e.toString());
}

for (int i = 0; i < files.length; i++) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open("ringtone/" + files[i]);
out = new FileOutputStream(basepath + "/ringtone/" + files[i]);

byte[] buffer = new byte[65536 * 2];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
out.flush();
out.close();
out = null;
Log.d(LOG_TAG, "Ringtone File Copied in SD Card");
} catch (Exception e) {
Log.e(LOG_TAG, "ERROR: " + e.toString());
}
}

Then read your file by the path:

File ringFile = new File(Environment.getExternalStorageDirectory().toString() + "/ringtone", "fileName.mp3");

There you go. You have a copy of file object of your asset file. Hope this helps.

Where is the appropriate location for shader files in android: Assets, Raw, or somewhere else?

Honestly, I would do neither. Storing them in your code itself is probably the safest and most secure method of doing it. (Making them public static final String variables in some class, that is.) I'd probably build them using StringBuilder when the app starts or when the class is initialized.

If you feel more comfortable with storing the files themselves, use assets. Using res/raw has restrictions on filenames, and you cannot use subdirectories in raw.

Storage limit in raw and asset folder in android

There is no limits on any resources now after Android2.3 release the new aapt.The limitation is caused by the older aapt for system peformance reason and should not happen now.

Android systems files , resource and assets in particular

The assets and res folders are part of the structure of the .apk file. This isn't something that can be changed by the application on the phone. You can create a folder structure in the phone's file system, but that will need to be accessed from your app by the file access api, not as assets or resource identifiers.



Related Topics



Leave a reply



Submit