• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Totally confused. Trying to save / load files in Android

edzillion

Lurker
Hi

I have read through numerous tutorials, which all tell me a little bit but I am lost on the overall picture. So how about I start at the start.

I have a Java app I wrote that saves a string of data as a csv file: data.txt - I want to be able to open this file in my android app, so the first thing I did was use the 'push' option in eclipse to save it onto the device. i could save it and view it on the sdcard, but when trying to dave to the app data folder (data/data/package) it seems to work but I cannot see the file there.

So after reading around I saw this piece of code:

Code:
        try { // catches IOException below
            final String TESTSTRING = new String("Hello Android");
            
            // ##### Write a file to the disk #####
            /* We have to use the openFileOutput()-method
            * the ActivityContext provides, to
            * protect your file from others and
            * This is done for security-reasons.
            * We chose MODE_WORLD_READABLE, because
            * we have nothing to hide in our file */
            FileOutputStream fOut = openFileOutput("samplefile.txt",
            MODE_WORLD_READABLE);
            OutputStreamWriter osw = new OutputStreamWriter(fOut);
            
            // Write the string to the file
            osw.write(TESTSTRING);
            /* ensure that everything is
            * really written out and close */
            osw.flush();
            osw.close();
            // ##### Read the file back in #####
            
            /* We have to use the openFileInput()-method
            * the ActivityContext provides.
            * Again for security reasons with
            * openFileInput(...) */
            FileInputStream fIn = openFileInput("samplefile.txt");
            InputStreamReader isr = new InputStreamReader(fIn);
            /* Prepare a char-Array that will
            * hold the chars we read back in. */
            char[] inputBuffer = new char[TESTSTRING.length()];
            // Fill the Buffer with data from the file
            isr.read(inputBuffer);
            // Transform the chars to a String
            String readString = new String(inputBuffer);
            
            // Check if we read back the same chars that we had written out
            boolean isTheSame = TESTSTRING.equals(readString);
            
            // WOHOO lets Celebrate =)
            Log.i("File Reading stuff", "success = " + isTheSame);
            
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
which works fine, though I cannot seem to see the file (I presume this is because it is located in the data/data/package/ folder and I cannot see that through eclipse DDMS.

>> Aside: I seem to have a lot of trouble accessing the file explorer using eclipse. It seems v. tempermental - sometimes I can see all the files and folders, other times I cannot open any of the folders (though it does list data, sdcard and system), with no discernable reason. I just did a test: loaded up the emulator on its own with wipe data set to 'on' and I can access the data folder but no subfolders of it :mad:

Anyway, When I try to use this same code to read my data.txt file from the sdcard I get the error: File sdcard/data.txt contains a path separator (I also tried the path /sdcard/data.txt but got the same error.

I read somewhere that openFileInput will not work with the sdcard.

This is really a kind of kludge just to test the data, since in the finished product the app wont read data from the sdcard. Am I missing something, should I serialise the data as XML? What is the best practice here?

Regards
ed
 
If your final app will not need to read the card, why not just create your data in a resource file as a string or array, or whatever form best matches the ultimate data source ?
 
Good point. I guess I was getting tunnel vision there;

What if, say, an application had episodic content? How would the app be updated? Can a server push an update other than the usual android market updates, or would the user have to initiate an update from a webserver or something ...
 
your app could call a remote web service on your server on startup or some other timed basis to check for available downloadable new content?
 
I need an answer as well - in fact I am running into a number of developers in this situation.

I do not want to package my 'data' files as a Resource - I want them out on the file system (and I don't want copies of the data files left as a Resource). I want to be able to update and add to the data files without having to reinstall the app.

For example - Mahjongg on Linux. You can change the tile set (or add to the tile sets) just by dropping a specially formated file into a specific directory. I want to do something like that. I want to have different data files the user can choose to 'install' into this directory (different data files should be different items in the Marketplace).

I have found a number of other developers wanting this capability, but I haven't seen any solutions yet. I'm new to Android ... can some give me an answer (or point me in the right direction) ?

P.S. I do not want to set up my own server and write my own data download code.
 
I need an answer as well - in fact I am running into a number of developers in this situation.

I do not want to package my 'data' files as a Resource - I want them out on the file system (and I don't want copies of the data files left as a Resource). I want to be able to update and add to the data files without having to reinstall the app.

For example - Mahjongg on Linux. You can change the tile set (or add to the tile sets) just by dropping a specially formated file into a specific directory. I want to do something like that. I want to have different data files the user can choose to 'install' into this directory (different data files should be different items in the Marketplace).

I have found a number of other developers wanting this capability, but I haven't seen any solutions yet. I'm new to Android ... can some give me an answer (or point me in the right direction) ?

P.S. I do not want to set up my own server and write my own data download code.


Lots of apps do this actually...

You could maybe start here:

PackageManager | Android Developers
 
Back
Top Bottom