Android Snippet – Read a File from Assets Folder in Android

Android Snippet – Read a File from Assets Folder in Android

In this android code snippet we are going to learn how to read a file from assets folder in android application development. You can also see our code snippet on reading and displaying the content of a text file in android raw directory.

The android assets folder can be use to store different kinds of file and images in android application development.

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: ReadFromAssetsFolder

Package: com.inducesmile.readfromassetsfolder

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 MainActivity.java file with the following code. Make sure you change the package name if you did not use the same package.

To test the application, we are going to create a text file in our project assets directory with a text content in it. The example application we will create will contain a one Button View and a TextView.

When the button is clicked, the text file content will be read and display on the TextView.

package readfromassetsfolder.inducesmile.com.readfromassetsfolder;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

public class MainActivity extends ActionBarActivity {

private TextView fileContent;
private Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

context = this;
fileContent = (TextView)findViewById(R.id.file_content);
Button readFileButton = (Button)findViewById(R.id.read_file);
readFileButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
readAndDisplayFileContentFromAssetsFolder();
}
});
}

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

@Override
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 == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

private void readAndDisplayFileContentFromAssetsFolder(){
AssetManager mgr = this.context.getAssets();
String filename = null;

try {
filename = "content.txt";
System.out.println("filename : " + filename);
InputStream in = mgr.open(filename, AssetManager.ACCESS_BUFFER);
String sHTML = StreamToString(in);
in.close();

fileContent.setText(sHTML);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static String StreamToString(InputStream in) throws IOException {
if(in == null) {
return "";
}
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
}
return writer.toString();
}
}

The activity_main.xml file content is shown below.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView
android:id="@+id/file_content"
android:text="@string/empty_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="212dp" />

<Button
android:id="@+id/read_file"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:layout_marginBottom="79dp"
android:layout_above="@+id/file_content"
android:layout_centerHorizontal="true" />

</RelativeLayout>

Save the file and run your project.

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

OTHER INTERESTING POSTS:

Add a Comment