Wow, I cant believe how many problems related to loading and saving to internal memory.
I find that I can only Save to a protected area related to my app.
I am not sure I can even create a directory in that area.
Storing to the SD I’ve mastered, but the requirements are that if no sdcard is available or has been removed the app continues be able to load and save user settings. I have 120 different data items so shared preferences are not the way. Cache is not persistent.
I currently have as a method :
public static void SaveUserPrefs()
{
try
{
//FileOutputStream fos = openFileOutput("userPrefs.dat", Context.MODE_PRIVATE);
FileOutputStream fos = new FileOutputStream( "userPrefs.dat");
BufferedOutputStream bos = new BufferedOutputStream(fos); //to get a buffered stream
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeFloat(versionNumber);
oos.writeInt(autoWifiStart);
oos.writeInt(autoAirPlane);
... many more items and file close
I get file not found exceptiom /userPrefs.dat Read only file system
You would think the same code would work either external or internal, but doesnt.
I have tried the comment out code also with same results.
Where and how do I save this data?
And do I need to do something different to load it?
I do have permission set to save_external_Storage.
On internal storage, this is correct.
Use
getFilesDir()to retrieve aFileobject pointing to your application’s area on internal storage, then callmkdirs()on theFileobject to create subdirectories. The latter is standard Java file I/O.There is no particular limit on the number of items in
SharedPreferences.SharedPreferencesare stored in an XML file.You have supplied an invalid path. Use
getFilesDir()to get theFileobject pointing to your application’s area on internal storage, then use the appropriateFileconstructor to create aFileobject pointing to your specific file on internal storage. Use that latterFileobject in yourFileOutputStreamconstructor. Again, other thangetFilesDir(), this is standard Java file I/O.That code does not work with external storage, either, as the path you have supplied is not on external storage.
Personally, I would never use
ObjectOutputStream. For 120 items, I would definitely consider a SQLite database, otherwise I would serialize my data to JSON or XML. Any of those options are easier to debug thanObjectOutputStream, IMHO.You will need to construct the appropriate
Fileobject pointing to the file in question for use with some type ofInputStream, following standard Java file I/O.WRITE_EXTERNAL_STORAGEis not necessary for writing to internal storage.