The Wayback Machine - https://web.archive.org/web/20240717024519/https://www.geeksforgeeks.org/assets-folder-in-android/
Open In App

Assets Folder in Android Studio

Last Updated : 05 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

It can be noticed that unlike Eclipse ADT (App Development Tools), Android Studio doesn’t contain an Assets folder in which we usually use to keep the web files like HTML. Assets provide a way to add arbitrary files like text, XML, HTML, fonts, music, and video in the application. If one tries to add these files as “resources“, Android will treat them into its resource system and you will be unable to get the raw data. If one wants to access data untouched, Assets are one way to do it. But the question arises is why in the asset folder? We can do the same things by creating a Resource Raw Folder. So let discuss how the assets folder is different from the Resource Raw folder? 

How the asset folder is different from the Resource Raw folder? 

In Android one can store the raw asset file like JSON, Text, mp3, HTML, pdf, etc in two possible locations:

  1. assets
  2. res/raw folder

assets and raw folder

Both of them appears to be the same, as they can read the file and generate InputStream as below

// From assets

assets.open(assetPathFilename)

// From res/raw

resources.openRawResource(resourceRawFilename)

But when to use which folder?

Below is some guidance that might be helpful to choose

1. Flexible File Name: (assets is better)

  • assets: The developer can name the file name in any way, like having capital letters (fileName) or having space (file name).
  • res/raw: In this case, the name of the file is restricted. File-based resource names must contain only lowercase a-z, 0-9, or underscore.

2. Store in subdirectory: (possible in assets)

  • assets: If the developer wants to categories the files into subfolders, then he/she can do it in assets like below.

assets folder

  • res/raw: In this case, files can only be in the root folder.

3. Compile-time checking: (possible in res/raw)

  • assets: Here, the way to read it into InputStream is given below. If the filename doesn’t exist, then we need to catch it.

assets.open(“filename”)

  • res/raw folder: Here, the way to read it into InputStream is:

resources.openRawResource(R.raw.filename)

So putting a file in the res/raw folder will provide ensure the correct file-name during compile time check.

4. List filenames at runtime: (possible in assets)

  • assets: If the developer wants to list all the files in the assets folder, he/she has used the list() function and provide the folder name or ” “ on the root folder as given below.

assets.list(FOLDER_NAME)?.forEach {  

   println(it)

}

  • res/raw: This is not possible in this folder. The developer has to know the filename during development, and not runtime.

So, in assets, one can read the filename during runtime, list them, and use them dynamically. In res/raw, one needs to code them ready, perhaps in the string resources file.

5. Filename accessible from XML: (possible in res/raw) 

  • assets: No simple way the developer can arrange an XML file (e.g. AndroidManifest.xml) to point to the file in the assets folder.
  • res/raw: In any XML files like in Java, the developer can access the file in res/raw using @raw/filename easily.

So if you need to access your file in any XML, put it in the res/raw folder. Let’s make a table to remember the whole scenario easily.

Scenario

Assets Folder

Res/Raw Folder
Flexible File Name

YES

NO

Store in subdirectory

YES

NO

Compile-time checking

NO

YES

List filenames at runtime

YES

NO

Filename accessible from XML

NO

YES

How to Create Assets Folder in Android Studio?

Now let’s discuss how to create an assets folder in the android studio. Below is the step-by-step process to create an assets folder in Android studio.

Step 1: To create an asset folder in Android studio open your project in Android mode first as shown in the below image.

Add Local HTML File

Step 2: Go to the app > right-click > New > Folder > Asset Folder and create the asset folder.

Add Local HTML File

Step 3: Android Studio will open a dialog box. Keep all the settings default. Under the target source set, option main should be selected. and click on the finish button.

Add Local HTML File

Step 4: Now open the app folder and you will find the assets folder by the name of “assets” as shown in the below image.

assets folder



Previous Article
Next Article

Similar Reads

How to Add Vector Assets in Android Studio?
Vector Assets in Android Studio helps to add material icons and import Scalable Vector Graphics and Adobe Photoshop Document files into your project as vector drawable resources. How Vector Asset is different from Image Asset? Image Asset Studio helps us create different types of icons with different densities and shows us exactly where they'll be
3 min read
How to fix "Android Studio doesn't see device" in Android Studio?
In Android Studio, sometimes the list of devices and emulators doesn't list your physical device when you try to plug it in. Or you may have faced a situation when you plugged the phone in for the first time, no dialog appeared asking if you trust the computer. But the laptop is already allowed to connect to the mobile device. However, the button R
4 min read
How to Fix "Android studio logcat nothing to show" in Android Studio?
Logcat Window is the place where various messages can be printed when an application runs. Suppose, you are running your application and the program crashes, unfortunately. Then, Logcat Window is going to help you to debug the output by collecting and viewing all the messages that your emulator throws. So, this is a very useful component for the ap
2 min read
How to Create Anim Folder & Animation File in Android Studio?
The animation is a method in which a collection of images is combined in a specific way and processed then they appear as moving images. Building animations make on-screen objects seem to be alive. To apply Animations to the Application sometimes we need to make an anim folder in Android Studio to store the animation file under the resource folder
2 min read
How to Add Image to Drawable Folder in Android Studio?
The resource folder is the most important folder because it contains all the non-code sources like images, XML layouts, UI strings for the android application. In Android Studio inside the res folder, one can find the drawable folder, layout folder, mipmap folder, values folder, etc. Among them, the drawable folder contains the different types of i
3 min read
How to Create New Package Inside Src Folder in Android Studio?
A package is a namespace that combines a set of relevant classes and interfaces. Conceptually one can consider packages as being similar to various folders on your computer. One might have HTML pages in one folder, images in another, and scripts or applications in yet another. Because in android, software written in the Java/Kotlin programming lang
2 min read
Resource Raw Folder in Android Studio
The raw (res/raw) folder is one of the most important folders and it plays a very important role during the development of android projects in android studio. The raw folder in Android is used to keep mp3, mp4, sfb files, etc. The raw folder is created inside the res folder: main/res/raw. So we will simply create it inside the res folder. But befor
4 min read
How to Create Menu Folder & Menu File in Android Studio?
Menus are the common user interface component in almost all the application. This interface provides a few options from which the user can select an option. There are 3 types of menu types available in Android: Options menu and app barContext menu and contextual action modePopup menuHow to define Menu in XML? In android, we can build the menu on ou
3 min read
How to Clone Android Project from GitHub in Android Studio?
Android Studio provides a platform where one can build apps for Android phones, tablets, Android Wear, Android TV, and Android Auto. Android Studio is the official IDE for Android application development, based on IntelliJ IDEA. One can develop Android Applications using Kotlin or Java as the Backend Language, and it provides XML for developing Fro
3 min read
How to Add OpenCV library into Android Application using Android Studio?
OpenCV is the huge open-source library for computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in today’s systems. By using it, one can process images and videos to identify objects, faces, or even the handwriting of a human. When it integrated with various libraries,
3 min read
Article Tags :