Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
The raw
folder in Android is used to store arbitrary files, saved in their raw form. These files are read in byte format, and you can include any type of files in the raw
folder, such as audio files, text files, databases, etc. These files will be packaged into the APK, and their content won't be changed, hence the name "raw."
Here's how you can use the raw
folder in Android Studio:
res
(resources) directory of your Android project, create a folder named raw
.sample.mp3
, data.txt
, etc.) into the raw
folder.You can access these files in your code using the generated R
class.
Example 1: Playing an audio file from the raw
folder:
val mediaPlayer = MediaPlayer.create(this, R.raw.sample) mediaPlayer.start()
Example 2: Reading a text file from the raw
folder:
val inputStream = resources.openRawResource(R.raw.data) val bufferedReader = BufferedReader(InputStreamReader(inputStream)) val text = bufferedReader.use { it.readText() }
Naming: The resource-naming convention is quite strict. File names can only contain lowercase a-z, 0-9, or underscore. They cannot contain spaces or special characters.
No sub-directories: Unlike the assets
folder, the raw
folder cannot have sub-directories. It's a flat structure.
When to use raw
vs assets
: If you need a strict resource ID for any file (like you get with drawables or XML resources), then use the raw
folder. If you have a large amount of data or files that you want to keep in their original format and organize into sub-folders, use the assets
folder.
File size limit: Be cautious of the size of the files you put into the raw
folder (or assets
for that matter), because these get packaged into the APK and can dramatically increase the size of your app.
Using the raw
folder is straightforward, but always consider the trade-offs in terms of APK size and organization when deciding where to place your app's resources.
Accessing raw resources in Android Studio:
// Accessing a raw resource file (e.g., raw_file.mp3) InputStream inputStream = getResources().openRawResource(R.raw.raw_file);
Loading data from raw folder in Android app:
// Loading a text file from raw folder InputStream inputStream = getResources().openRawResource(R.raw.text_file); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
Including videos/audio in the raw folder Android:
// Playing an audio file from raw folder MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.audio_file); mediaPlayer.start();
Accessing raw folder in Kotlin Android:
// Accessing a raw resource file in Kotlin val inputStream: InputStream = resources.openRawResource(R.raw.raw_file)
Raw folder and resource qualifiers in Android:
// Accessing a raw resource with a qualifier (e.g., raw_folder_hdpi) InputStream inputStream = getResources().openRawResource(R.raw.raw_folder_hdpi);