Android Snippet – Read a file from Raw Directory in Android

Android Snippet – Read a file from Raw Directory in Android

In this tutorial we will focus and learn how to read a file from android raw directory. The raw directory in android application is basically use to store media files. To use raw directory in your application, you will need to create a raw directory and put it inside the android resource folder.

If the raw directory is place anywhere outside the resource folder, it will not work.

If you are interested in reading non-media files, you can put the file in your application assets folder. A detailed android code snippet for reading files from the assets folder is provided here.

Before we start, the first thing I will do is to list the environment and tools I used in this android code snippet but feel free to use whatever environment or tools you are familiar with.

Windows 7

Android Studio

Samsung Galaxy Fame Lite

Min SDK 14

Target SDK 19

To create a new android application project, following the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: ReadFromRawFolder

Package: com.inducesmile.readfromrawfolder

Keep other default selections.

Continue to click on next button until Finish button is active, then click on Finish Button

When you are done with creating your project, copy and paste the following code snippet to your project. Replace the code in your file with the following code. Make sure you change the package name if you did not use the same package.

import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

private TextView fileContent;
private Context context;

protected void onCreate(Bundle savedInstanceState) {
context = this;
fileContent = (TextView)findViewById(;
Button readFileButton = (Button)findViewById(;
readFileButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
String returnedFileContent = readFileFromRawDirectory(R.raw.content);

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(, menu);
return true;

public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == {
return true;
return super.onOptionsItemSelected(item);

private String readFileFromRawDirectory(int resourceId){
InputStream iStream = context.getResources().openRawResource(resourceId);
ByteArrayOutputStream byteStream = null;
try {
byte[] buffer = new byte[iStream.available()];;
byteStream = new ByteArrayOutputStream();
} catch (IOException e) {
return byteStream.toString();

The code above uses a Button and TextView control in the layout. When the button is clicked, it will read the content of the file (content.txt) stored in the raw folder and display its content to the TextView.

The activity_main.xml file content is shown below.

<RelativeLayout xmlns:android=""
xmlns:tools="" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

android:layout_marginBottom="212dp" />

android:text="New Button"
android:layout_centerHorizontal="true" />


Save the file and run your project. If everything works fine you will see an interface like the one showed below.

Android Snippet – Read a file from Raw Directory in Android

You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact and I will send it to you.

Remember to subscribe with your email so that you will be among the first to receive our new post once it is published

Add a Comment