[android-developers] Loading and Saving Settings to a file

2012-02-06 Thread John-Marc Desmarais
Hi,

I'm not sure what I'm doing wrong here:

I have an Override on onPause to save settings to a file and an
Override on onResume, and onCreate to load them back up from the file.

The onPause method looks like this:
@Override
public void onPause()
{
super.onPause();

// save configuration settings
if (cfg != null)
{
byte[] settingsBuffer = 
cfg.toSerializedBytes(getBaseContext());
Log.v(LogName.onPause, LogName.onPause + : settings 
Buffer :  +
settingsBuffer.length);
try
{
FileOutputStream fos = 
openFileOutput(settingsFile,
getBaseContext().MODE_WORLD_READABLE);
fos.write(settingsBuffer);
fos.getFD().sync();

fos.close();
}
catch (FileNotFoundException e)
{
Log.e(LogName.onPause, LogName.onPause + : 
File not found.  +
e.getMessage());
}
catch (IOException e)
{
Log.e(LogName.onPause, LogName.onPause + : IO 
Error.  + e.getMessage());
}
}
}

The load method looks like this:
public void loadSettings()
{
File inputFile = new File(settingsFile);
if (inputFile.exists())
{
byte[] settingsBuffer = new byte[(int) 
(inputFile.length())];
try
{
FileInputStream fis = 
openFileInput(settingsFile);
fis.read(settingsBuffer);
cfg = 
RXConfigurationSettings.fromSerializedBytes(settingsBuffer);
if (cfg != null)
{
cfg.populateTable((TableLayout)
findViewById(R.id.tableLayoutConfigurationSettingsTable),
getBaseContext());
}
fis.close();
}
catch (FileNotFoundException e)
{
Log.e(LogName.onResume, LogName.onResume + : 
File not found.  +
e.getMessage());
}
catch (IOException e)
{
Log.e(LogName.onResume, LogName.onResume + : 
IO Error.  + e.getMessage());
}
}
else
{
Log.e(LogName.className, Input FILE not found.);
}
}


The loadSettings is called from onCreate and onResume but I seem to
always hit the else condition and logcat always shows Input FILE not
found..

Can anyone provide insight into this problem?

Thanks
-jm

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] Loading and Saving Settings to a file

2012-02-06 Thread TreKing
On Mon, Feb 6, 2012 at 11:15 AM, John-Marc Desmarais j...@ieee.org wrote:

 I have an Override on onPause to save settings to a file


Why not just use SharedPreferences?


 Can anyone provide insight into this problem?


When you check the file existing, you use the raw file name, not relative
to your data directory. Of course, that does not exist, as the file is
relative to your data directory.

Your subsequent call to openFileInput would likely succeed.

-
TreKing http://sites.google.com/site/rezmobileapps/treking - Chicago
transit tracking app for Android-powered devices

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] Loading and Saving Settings to a file

2012-02-06 Thread John-Marc Desmarais
Hi,

Thank you for your reply. I have a couple follow up comments/questions
related to the responses. I have included these inline.

On Mon, Feb 6, 2012 at 12:25 PM, TreKing treking...@gmail.com wrote:
 On Mon, Feb 6, 2012 at 11:15 AM, John-Marc Desmarais j...@ieee.org wrote:

 I have an Override on onPause to save settings to a file


 Why not just use SharedPreferences?

I felt it would be quicker to serialize my class into a byte stream
than saving each configuration parameter separately and I couldn't
find the ability to store a byte stream using SharedPreferences.


 Can anyone provide insight into this problem?


 When you check the file existing, you use the raw file name, not relative
 to your data directory. Of course, that does not exist, as the file is
 relative to your data directory.

 Your subsequent call to openFileInput would likely succeed.

How do I handle the
byte[] settingsBuffer = new byte[(int) inputFile.length];
without opening a File?

i.e. How would I figure out in advance the size of the byte buffer I
need to receive my file? Or, what path can I append to my open file to
allow it to find the settingsFile, or is it not that easy?

-jm

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] Loading and Saving Settings to a file

2012-02-06 Thread TreKing
On Mon, Feb 6, 2012 at 11:36 AM, John-Marc Desmarais j...@ieee.org wrote:

 How do I handle the byte[] settingsBuffer = new byte[(int)
 inputFile.length]; without opening a File?


You get the stream of data as the returned value of openFileInput. Then you
can then read that into a byte array of the input's length. So your
settings buffer would be initialized AFTER you open the input stream, using
the stream's length.

-
TreKing http://sites.google.com/site/rezmobileapps/treking - Chicago
transit tracking app for Android-powered devices

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] Loading and Saving Settings to a file

2012-02-06 Thread John-Marc Desmarais
On Mon, Feb 6, 2012 at 12:56 PM, TreKing treking...@gmail.com wrote:
 On Mon, Feb 6, 2012 at 11:36 AM, John-Marc Desmarais j...@ieee.org wrote:

 How do I handle the byte[] settingsBuffer = new byte[(int)
 inputFile.length]; without opening a File?


 You get the stream of data as the returned value of openFileInput. Then you
 can then read that into a byte array of the input's length. So your settings
 buffer would be initialized AFTER you open the input stream, using the
 stream's length.

Hi

Based on your first response I have tried the following:
public void onPause()
{
super.onPause();
// save configuration settings
SharedPreferences.Editor settingsEditor =
getSharedPreferences(settingsFile, 0).edit();
settingsEditor.putString(RXConfigurationSettings,
Base64.encodeToString(cfg.toSerializedBytes(getBaseContext()),
Base64.DEFAULT));
}

public void loadSettings()
{
SharedPreferences settings = getSharedPreferences(settingsFile, 
0);
if (settings.contains(RXConfiguration))
{
cfg = 
RXConfigurationSettings.fromSerializedBytes(Base64.decode(settings.getString(RXConfigurationSettings,
), Base64.DEFAULT));
}

else
{
Log.e(LogName.className, Input settings not found.);
}
}
And am now getting Input settings not found when closing and
reopening my application.

-jm

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] Loading and Saving Settings to a file

2012-02-06 Thread TreKing
On Mon, Feb 6, 2012 at 12:04 PM, John-Marc Desmarais j...@ieee.org wrote:

 And am now getting Input settings not found when closing and
 reopening my application.


Read the class overview:
http://developer.android.com/reference/android/content/SharedPreferences.Editor.html


-
TreKing http://sites.google.com/site/rezmobileapps/treking - Chicago
transit tracking app for Android-powered devices

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] Loading and Saving Settings to a file

2012-02-06 Thread John-Marc Desmarais
Yup.

commit() still works as advertised.

Thank you,
-jm

On Mon, Feb 6, 2012 at 1:19 PM, TreKing treking...@gmail.com wrote:
 On Mon, Feb 6, 2012 at 12:04 PM, John-Marc Desmarais j...@ieee.org wrote:

 And am now getting Input settings not found when closing and
 reopening my application.


 Read the class overview:
 http://developer.android.com/reference/android/content/SharedPreferences.Editor.html


 -
 TreKing - Chicago transit tracking app for Android-powered devices

 --
 You received this message because you are subscribed to the Google
 Groups Android Developers group.
 To post to this group, send email to android-developers@googlegroups.com
 To unsubscribe from this group, send email to
 android-developers+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en