[Xamarin] 關於Internal Storage ,存取App內部使用資料

2013-08-09

最近在開發App,會使用到必須要處理一些App所使用的資料,上網路查一下Android 得作法,包含我自己也實作了一下,可能是因為對Java || Android 不是很孰悉,常常錯在 java.lang.illegalargumentexception  這上面,查一下Xamarin 論壇: http://forums.xamarin.com/discussion/333/how-to-save-a-string-on-internal-isolated-storage-with-mono-for-android
在建立檔案夾的時候也會出現,所以我看了一下,乾脆就依照Xamarin 的特性使用System.IO 下的方法來處理..
介紹一下今天案例:

2013-08-09_114347

就是我將資料寫入至 internal storage 預設路徑下根的 note.txt 並且讀取出來,之後我也嘗試建立一個play檔案夾,並且寫檔note.txt之後讀取出來


//  Internal Storage Methods.
 
/// <summary>
/// 將String寫入至Internal Storage 
/// </summary>
/// <param name="filename"></param>
/// <param name="content"></param>
public void WriteAllText(string filename, string content)
{
 
    //如果前面沒有 / 則補上
    if (filename[0] != '/')
    {
        filename = "/" + filename;
    }
 
    //預設讀取 data/data/[package name]/files
    if (filename.Contains("/"))
    {    
        Directory.CreateDirectory(GetFileStreamPath("") + filename.Substring(0, filename.LastIndexOf('/')));
    }
 
    System.IO.File.WriteAllText(GetFileStreamPath("") + filename, content);
}
 
/// <summary>
/// 將文字讀取出來
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public string ReadAllText(string filename)
{
    try
    {
 
        if (filename[0] != '/')
        {
            filename = "/" + filename;
        }
        return System.IO.File.ReadAllText(GetFileStreamPath("") + filename);
 
 
    }
    catch (Exception ex)
    {
        throw;
    }
 
}

這兩隻function,如果你傳入的路徑,是沒有檔案夾的,會自動幫忙建立
寫入/讀取 根 /data/data/[packagename]/files/ 的部分:


var btnSave = FindViewById<Button>(Resource.Id.btnSave);
btnSave.Click += delegate
{
    WriteAllText("note.txt", "大家好我是當麻許!!" + DateTime.Now.ToString());
 
};
 
 
var btnRead = FindViewById<Button>(Resource.Id.btnRead);
btnRead.Click += delegate
{
    try
    {
        var message = ReadAllText("note.txt");
        Toast.MakeText(this, message, ToastLength.Short).Show();
 
    }
    catch (Exception ex)
    {
        Toast.MakeText(this, ex.Message, ToastLength.Short).Show();
    }
};

結果 :

shot_000006

用文件管理器檢視:
shot_000003

  寫入至檔案夾中的:


var btnDirWrite = FindViewById<Button>(Resource.Id.btnDirWrite);
btnDirWrite.Click += delegate
{
    try
    {
        WriteAllText(@"play/note.txt", "大家好我是檔案夾中的當麻許!!" + DateTime.Now.ToString());
 
    }
    catch (Exception ex)
    {
        Toast.MakeText(this, ex.Message, ToastLength.Short).Show();
    }
};
 
var btnDirRead = FindViewById<Button>(Resource.Id.btnDirRead);
btnDirRead.Click += delegate
{
    try
    {
        var res = ReadAllText(@"play/note.txt");
        Toast.MakeText(this, res, ToastLength.Short).Show();
    }
    catch (Exception ex)
    {
        Toast.MakeText(this, ex.Message, ToastLength.Short).Show();
    }
};

shot_000012

shot_000011

ps.請注意瞜,因為基本上只有APP 本身可以存取Internal Storage 的部分,所以我手機是因為root 過才能看到,至於傳統android 寫法中可以讓其他app存取的屬性MODE_WORLD_WRITEABLE , MODE_WORLD_READABLE 接下來android 官方建議 也是 Android provides a way for you to expose even your private data to other applications — with a content provider. A content provider is an optional component that exposes read/write access to your application data, subject to whatever restrictions you want to impose. For more information about using content providers, see the Content Providers documentation. 所以不建議用這種方式跟其他APP 開放資料溝通瞜..

參考資料:
http://developer.android.com/guide/topics/data/data-storage.html
http://developer.android.com/reference/android/content/Context.html#MODE_WORLD_WRITEABLE

Sample :



當麻許的超技八 2014 | Donma Hsu Design.