1. Check out our companion app, Forums for Android! Download from Google Play

Totally confused. Trying to save / load files in Android

Discussion in 'Application Development' started by edzillion, Nov 11, 2009.

  1. edzillion

    edzillion New Member
    Thread Starter
    25

    Nov 11, 2009
    3
    0
    25
    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 (Text):
    1.         try { // catches IOException below
    2.             final String TESTSTRING = new String("Hello Android");
    3.            
    4.             // ##### Write a file to the disk #####
    5.             /* We have to use the openFileOutput()-method
    6.             * the ActivityContext provides, to
    7.             * protect your file from others and
    8.             * This is done for security-reasons.
    9.             * We chose MODE_WORLD_READABLE, because
    10.             * we have nothing to hide in our file */
    11.             FileOutputStream fOut = openFileOutput("samplefile.txt",
    12.             MODE_WORLD_READABLE);
    13.             OutputStreamWriter osw = new OutputStreamWriter(fOut);
    14.            
    15.             // Write the string to the file
    16.             osw.write(TESTSTRING);
    17.             /* ensure that everything is
    18.             * really written out and close */
    19.             osw.flush();
    20.             osw.close();
    21.             // ##### Read the file back in #####
    22.            
    23.             /* We have to use the openFileInput()-method
    24.             * the ActivityContext provides.
    25.             * Again for security reasons with
    26.             * openFileInput(...) */
    27.             FileInputStream fIn = openFileInput("samplefile.txt");
    28.             InputStreamReader isr = new InputStreamReader(fIn);
    29.             /* Prepare a char-Array that will
    30.             * hold the chars we read back in. */
    31.             char[] inputBuffer = new char[TESTSTRING.length()];
    32.             // Fill the Buffer with data from the file
    33.             isr.read(inputBuffer);
    34.             // Transform the chars to a String
    35.             String readString = new String(inputBuffer);
    36.            
    37.             // Check if we read back the same chars that we had written out
    38.             boolean isTheSame = TESTSTRING.equals(readString);
    39.            
    40.             // WOHOO lets Celebrate =)
    41.             Log.i("File Reading stuff", "success = " + isTheSame);
    42.            
    43.         } catch (IOException ioe) {
    44.             ioe.printStackTrace();
    45.         }
    46.     }
    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
     

    Advertisement

  2. galt

    galt Well-Known Member
    36

    Oct 26, 2009
    153
    2
    36
    Florida, USA
    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 ?
     
  3. edzillion

    edzillion New Member
    Thread Starter
    25

    Nov 11, 2009
    3
    0
    25
    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 ...
     
  4. khooke

    khooke Member
    25

    Aug 31, 2009
    7
    0
    25
    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?
     
  5. eialbur

    eialbur New Member
    5

    Mar 29, 2011
    4
    0
    5
    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.
     
  6. alostpacket

    alostpacket Over Macho Grande?
    513

    Nov 29, 2009
    7,978
    3,604
    513
    Android App Developer
    NY

    Lots of apps do this actually...

    You could maybe start here:

    PackageManager | Android Developers
     

Share This Page

Loading...