Use Static Resources
Description
Here is the three steps to use static resource:
- Put files to your package's res/raw folder
- Use the getResources() method from the Activity class to return a Resources object
- Use its openRawResource() method to open the file contained in the res/raw folder
Example
package com.java2s.myapplication3.app;
//from w w w. j a v a2 s . com
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MainActivity extends Activity {
EditText textBox;
static final int READ_BLOCK_SIZE = 100;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textBox = (EditText) findViewById(R.id.txtText1);
InputStream is = this.getResources().openRawResource(R.raw.textfile);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String str = null;
try {
while ((str = br.readLine()) != null) {
Toast.makeText(getBaseContext(),
str, Toast.LENGTH_SHORT).show();
}
is.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Note
The resource ID of the resource stored in the res/raw
folder is named after its filename without its
extension.
For example, if the text file is textfile.txt, then its resource ID is R.raw.textfile.