Tuesday, January 7, 2014

Android: Save Program Data Permanently (in External Storage)

When the applications get more complex, one for sure wants to save some data permanently in the phone so they can for example be reused when next executing the program. For this Android provides essentially 2 storage areas, the Internal and the External Storage.
In the Internal Storage the data is saved such that only the application itself can access it, when uninstalling it the data is deleted automatically.
In contrast to that the External Storage is a public storage which is accessible to everyone, so also from the user from the computer for example.
The terms "internal" and "external" storage stem from the origin times of Android, where mobile phones had a small internal storage and maybe an external one in form of a SD - card for example. Today though both storages are mostly stored in the "real" internal storage of the smart phone, the difference is now just the access right.
Nevertheless it should still be checked if the External Storage is available ("mounted").
Since I had some problems with the Internal Storage and I like to be able to edit my data per hand, I mostly use the External Storage, over what I will write in this post.
So that the app is able to use the External Storage, first the permission WriteExternalStorage has to be set in the AndroidManifest.xml.
The path to the External Storage we get with Android.OS.Environment.ExternalStorageDirectory.Path.
Then we can access it like under Windows, write files etc.
In the following little sample we first check via the property ExternalStorageState, whether the External Storage is available.
Then we create the file "TestFile.txt" in the folder "ExternalStorageTest" using a StreamWriter and write a line in it. Like on Windows too all folders have to exist, therefor we check this via a Directory object and create the folder in case.
Since these things should be known from the Windows programming, here directly the complete code:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

using System.IO;

namespace ExternalStorageExample
     [Activity (Label = "ExternalStorageExample", MainLauncher = true)]
     public class MainActivity : Activity
          protected override void OnCreate (Bundle bundle)
               base.OnCreate (bundle);

               // Set our view from the "main" layout resource
               SetContentView (Resource.Layout.Main);

               if (Android.OS.Environment.ExternalStorageState == "mounted") {
                    string Path = Android.OS.Environment.ExternalStorageDirectory.Path + "/ExternalStorageTest/";

                    if (!Directory.Exists (Path))
                         Directory.CreateDirectory (Path);

                    StreamWriter sw = new StreamWriter (new FileStream (Path + "TestFile.txt", FileMode.Create));
                    sw.WriteLine ("Test");
                    sw.Close ();

No comments:

Post a Comment