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

Resource Raw Folder in Android Studio

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:

1. Creating the raw folder:

  • Inside the res (resources) directory of your Android project, create a folder named raw.

2. Adding a file:

  • Place your desired files (e.g., sample.mp3, data.txt, etc.) into the raw folder.

3. Accessing files from 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() }

Points to remember:

  1. 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.

  2. No sub-directories: Unlike the assets folder, the raw folder cannot have sub-directories. It's a flat structure.

  3. 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.

  4. 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.

  1. Accessing raw resources in Android Studio:

    • Description: Explains how to access resources from the raw folder in Android Studio.
    • Example Code:
      // Accessing a raw resource file (e.g., raw_file.mp3)
      InputStream inputStream = getResources().openRawResource(R.raw.raw_file);
      
  2. Loading data from raw folder in Android app:

    • Description: Shows how to load data, such as images or text, from the raw folder into the Android app.
    • Example Code:
      // Loading a text file from raw folder
      InputStream inputStream = getResources().openRawResource(R.raw.text_file);
      BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
      
  3. Including videos/audio in the raw folder Android:

    • Description: Illustrates how to include video or audio files in the raw folder and play them in the app.
    • Example Code:
      // Playing an audio file from raw folder
      MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.audio_file);
      mediaPlayer.start();
      
  4. Accessing raw folder in Kotlin Android:

    • Description: Demonstrates how to access resources from the raw folder in Android using Kotlin.
    • Example Code:
      // Accessing a raw resource file in Kotlin
      val inputStream: InputStream = resources.openRawResource(R.raw.raw_file)
      
  5. Raw folder and resource qualifiers in Android:

    • Description: Discusses the use of resource qualifiers for the raw folder to handle different device configurations.
    • Example Code:
      // Accessing a raw resource with a qualifier (e.g., raw_folder_hdpi)
      InputStream inputStream = getResources().openRawResource(R.raw.raw_folder_hdpi);