Wednesday, July 25, 2012

Android - file I/O


Here is my code reading/writing some simple JSON. Hope it helps you out!
private void writeToFile(String jsonString) {
Log.d(TAG, "Writing JSON to file: " + jsonString);
try {
FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fos);

osw.write(jsonString);
osw.flush();
osw.close();
fos.close();

} catch (FileNotFoundException e) {
throw new WorldClockException(e);
} catch (IOException e) {
throw new WorldClockException(e);
}
}

Open the file in private mode - only owning application may read/write to such a file. Write the string, flush and close.

The close() should ideally be in a finally block but I'm still trying to figure out Android so this is not perfect!

Also posted on iRahul.com

Tuesday, July 17, 2012

Thursday, July 12, 2012

Android - reading and writing files

Playing around with Android these days and it turns out for simple file I/O I had to search around a bit. Here is my code reading/writing some simple JSON. Hope it helps you out!
private void writeToFile(String jsonString) {
  Log.d(TAG, "Writing JSON to file: " + jsonString);
  try {
   FileOutputStream fos = context.openFileOutput(FILENAME,     Context.MODE_PRIVATE);
   OutputStreamWriter osw = new OutputStreamWriter(fos);

   osw.write(jsonString);
   osw.flush();
   osw.close();
   fos.close();

  } catch (FileNotFoundException e) {
   throw new WorldClockException(e);
  } catch (IOException e) {
   throw new WorldClockException(e);
  }
 }

Open the file in private mode - only owning application may read/write to such a file. Write the string, flush and close.

The close() should ideally be in a finally block but I'm still trying to figure out Android so this is not perfect!

Part of my bigger WorldClock project at GitHub if you are interested to contribute!

Wednesday, July 11, 2012

Android App - World Clock

My first attempt at writing an Android app!
On the Google Play store:

Get it on Google Play

More details and open-source code at AndroidOpenSourceApps.org

I appreciate any feedback.

Tuesday, July 10, 2012

World Clock v0.5

Half way there! This is a totally free app with NO ads. The app does not request any permissions either.


Current features:
  • Add/remove/edit timezone selections
  • Search list using a string as you type
  • Flag icons for countries 
Download the Source file from GitHub and get the app on Google Play. Please donate using the support box on the left!

Get it on Google Play

Roadmap - To get to v1.0, I am planning to add App Widgets so you can see your selections on the home screen. Possibly even add some preferences to control the time display format.

I have manually tested the app so far and I'm sure you can find bugs so please submit a report (and also help fix it!) If you know Android test automation and would like to help I have created a test project that can use your help. Also I am by no means an Android expert so would appreciate any suggestions on improve the code/style/performance. I will post some code discussions on my blog occasionally so check it out.

Adding a new selection: you can type a search string, then choose a custom name and save.




Modify - edit or remove - tap and hold to bring up the context menu.