Using files as raw resources in Android

Why need such type of resources, we can get data from /assets, /res folders and now I am going to show /res/raw folder.

It is simple, raw resources are not compiled by the platform, they available as raw files and these files can refer to any type of files. When you need to include raw files in your application you can do so using /res/raw resources location. When you will place files here, they will be available as raw resources. Getting raw resources is similar to getting files, you need InputStream, and you use Context. Context you will use for getting Resources and after that you can get your raw file. It is simple:

InputStream inputStream = getResources().openRawResource(R.raw.rawresource);

I am going to show example how you can access and read files from /res/raw folder, file for my example which is located under raw folder is simple text file.
rawExample

Okay. Let’s look to the example:

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

/**
 * This example show how to use raw files from /raw folder
 * @author FaYnaSoft Labs
 *
 */
public class Main extends Activity {
	private static String LOG_APP_TAG = "tag";

	private EditText editField;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        editField = (EditText) findViewById(R.id.textId);

        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				InputStream inputStream = null;
				try {
					inputStream = getResources().openRawResource(R.raw.hello_world);
					byte[] reader = new byte[inputStream.available()];
					while (inputStream.read(reader) != -1) {}
					editField.setText(new String(reader));
					editField.setSelection(editField.getText().length());
				} catch(IOException e) {
					Log.e(LOG_APP_TAG, e.getMessage());
				} finally {
					if (inputStream != null) {
						try {
							inputStream.close();
						} catch (IOException e) {
							Log.e(LOG_APP_TAG, e.getMessage());
						}
					}
				}
			}
		});
    }
}

As you can see after receiving InputStream object I can do what I want. Here I read this file’s data and set this data to EditText component.

Note: don’t forget that raw files can be not only text files they can be in any formats.

Code from this article

Was it useful for you? Leave a comment.

Share

6 comments to Using files as raw resources in Android

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna, tincidunt vitae molestie nec, molestie at mi. Nulla nulla lorem, suscipit in posuere in, interdum non magna.