tags:

views:

31

answers:

2

Hello. I have several files stored in my project /res/values folder, is there any way to open and read these files from my android application? Each file contains text informations about one level of my game. I really appreciate any help.

+1  A: 

As far I know you can either access files within the directory "files" from your project directory or from the SD-Card. But no other files

EDIT

FileInputStream in = null;
InputStreamReader reader = null;
try {
    char[] inputBuffer = new char[256];
    in = openFileInput("myfile.txt");
    reader = new InputStreamReader(in);
    reader.read(inputBuffer);
    String myText = new String(inputBuffer);
} catch (Exception e) {;}
finally {
    try {
        if (reader != null)reader.close();
    } catch (IOException e) {; }
    try {
        if (in != null)in.close();
    } catch (IOException e) {;}
}

Then your file will be located in: /data/data/yourpackage/files/myfile.txt

Radek Suski
So i can create 'files' directory in my project and get access to files stored there from my application? Ok that is exacly what I need. But what is the path to this directory? I try to open file with path like these: "/files/1.lvl" but this is not working. Any ideas?
Kubeczek
AFIR, you don't even have to create this directory. See my edit
Radek Suski
Your example is very instructive, thanks a lot for that!But It seems we have a little misunderstanding. I was needed to create some files with 'static-read-only' map data for game.I mean: instead of write my own map editor I decided to create text files where each character represent one object on the map in the game. So I create several files, with data like this and now I need to propagate them with my game .apk file so I can easy read these files while map creating. I never don't want to edit or create this files during my app running, I create these only once before compiling program.
Kubeczek
A: 

I find what I needed here:
http://developer.android.com/guide/topics/data/data-storage.html
"If you want to save a static file in your application at compile time, save the file in your project res/raw/ directory. You can open it with openRawResource(), passing the R.raw. resource ID. This method returns an InputStream that you can use to read the file (but you cannot write to the original file). "
Sorry if My question was not clear.
And big thanks to Radek Suski for some additional information and example. I appreciate that.

Kubeczek