Resource Raw Folder in Android Studio
Last Updated: 25 Feb, 2021The raw (res/raw) folder in Android Studio is a crucial component during the development of Android projects. It is specifically used to store files such as mp3, mp4, and sfb files. This folder is located within the 'res' folder: main/res/raw. Before creating the raw folder, it's essential to understand the role of the asset folder in Android, which serves a similar purpose.
raw (res/raw)resmain/res/rawhow the resource raw folder is different from the assets folder?How the Resource raw folder is different from the Assets folder?
In Android, one can store raw asset files like JSON, Text, mp3, HTML, PDF, etc., in two primary locations:
two
While both locations may seem similar in their ability to read files and generate InputStream, there are distinctions in their usage.
But when to use which folder?
Here are some guidelines to help you decide:
- Flexible File Name:
- assets: Developers have more flexibility in naming files, allowing the use of capital letters or spaces in filenames.
- res/raw: File names are restricted to lowercase letters a-z, numbers 0-9, or underscores.
Storing and Handling Files in Android Development
When working on an Android application, developers need to carefully manage how files are stored and accessed within the project. Let's explore the key considerations:
Storing Files in Assets
- Assets: Developers can name files as they wish, including variations like using capital letters or spaces.
- Res/raw: File names in this directory are restricted to lowercase letters, numbers, or underscores only.
- Store in subdirectory (possible in assets): Developers can organize files into subfolders within the assets directory for better categorization.
For example, in the assets folder, a developer can create subfolders to group related files together, enhancing organization.
Compile-time Checking in Res/raw
- Assets: Instructions for reading files into InputStream are provided, with error handling for non-existent files.
- Res/raw folder: Details on reading files into InputStream for compile-time checking purposes.
By placing files in the res/raw folder, developers ensure that file names are correct and can be verified during compilation.
List Filenames at Runtime in Assets
- Assets: Developers can use the list() function to display all files in the assets folder, specifying the folder name or leaving it blank for the root folder.
By utilizing the list() function, developers can dynamically retrieve and display filenames from the assets folder during runtime.
Listing Files in Assets and Res/Raw Folders
- Assets: To display all files in the assets folder, developers use the list() function by providing the folder name or root folder.
- Res/Raw: Unlike assets, listing files in this folder is not feasible during runtime; filenames must be known during development.
Accessing Files in Different Folders
- Assets: Files can be read and used dynamically at runtime, offering flexibility in file access.
- Res/Raw: Files need to be pre-coded, typically in the string resources file, for access.
File Accessibility from XML
- Assets: No direct method to link XML files (e.g., AndroidManifest.xml) to assets; requires alternative approaches.
- Res/Raw: File access in XML files, such as in Java, is straightforward using @raw/filename notation.
Comparison Table
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 Resource Raw Folder in Android Studio?
Now, let's discuss the process of creating the Resource Raw folder in Android Studio.
- Resource Raw
- Begin by opening your project in Android mode. You can refer to the image below for guidance.

- In Android Studio, navigate to app > res. Right-click, then go to New > Android Resource Directory, as shown in the image below.

- A pop-up window will appear. In the Resource type dropdown, select "raw."

- After selecting "raw," click OK and keep the settings as they are.

- You will now see that the raw folder has been successfully created in app > res > raw, as depicted in the image.

Please Login to comment...
If you found this information useful, don't forget to login and share your thoughts.
LoginLike