您的位置:首页 > 移动开发

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

2013-11-27 01:13 417 查看
最近在開發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下的方法來處理..介紹一下今天案例:





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

//InternalStorageMethods.


///<summary>

///將String寫入至InternalStorage

///</summary>

///<paramname="filename"></param>

///<paramname="content"></param>

publicvoidWriteAllText(stringfilename,stringcontent)

{


//如果前面沒有/則補上

if(filename[0]!='/')

{

filename="/"+filename;

}


//預設讀取data/data/[packagename]/files

if(filename.Contains("/"))

{

Directory.CreateDirectory(GetFileStreamPath("")+filename.Substring(0,filename.LastIndexOf('/')));

}


System.IO.File.WriteAllText(GetFileStreamPath("")+filename,content);

}


///<summary>

///將文字讀取出來

///</summary>

///<paramname="filename"></param>

///<returns></returns>

publicstringReadAllText(stringfilename)

{

try

{


if(filename[0]!='/')

{

filename="/"+filename;

}

returnSystem.IO.File.ReadAllText(GetFileStreamPath("")+filename);



}

catch(Exceptionex)

{

throw;

}


}


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

varbtnSave=FindViewById<Button>(Resource.Id.btnSave);

btnSave.Click+=delegate

{

WriteAllText("note.txt","大家好我是當麻許!!"+DateTime.Now.ToString());


};



varbtnRead=FindViewById<Button>(Resource.Id.btnRead);

btnRead.Click+=delegate

{

try

{

varmessage=ReadAllText("note.txt");

Toast.MakeText(this,message,ToastLength.Short).Show();


}

catch(Exceptionex)

{

Toast.MakeText(this,ex.Message,ToastLength.Short).Show();

}

};


結果:



用文件管理器檢視:





寫入至檔案夾中的:

varbtnDirWrite=FindViewById<Button>(Resource.Id.btnDirWrite);

btnDirWrite.Click+=delegate

{

try

{

WriteAllText(@"play/note.txt","大家好我是檔案夾中的當麻許!!"+DateTime.Now.ToString());


}

catch(Exceptionex)

{

Toast.MakeText(this,ex.Message,ToastLength.Short).Show();

}

};


varbtnDirRead=FindViewById<Button>(Resource.Id.btnDirRead);

btnDirRead.Click+=delegate

{

try

{

varres=ReadAllText(@"play/note.txt");

Toast.MakeText(this,res,ToastLength.Short).Show();

}

catch(Exceptionex)

{

Toast.MakeText(this,ex.Message,ToastLength.Short).Show();

}

};









ps.請注意瞜,因為基本上只有APP本身可以存取InternalStorage的部分,所以我手機是因為root過才能看到,至於傳統android寫法中可以讓其他app存取的屬性MODE_WORLD_WRITEABLE,MODE_WORLD_READABLE接下來android官方建議也是Androidprovidesawayforyoutoexposeevenyourprivatedatatootherapplications—withacontentprovider.Acontentproviderisanoptionalcomponentthatexposesread/writeaccesstoyourapplicationdata,subjecttowhateverrestrictionsyouwanttoimpose.Formoreinformationaboutusingcontentproviders,seetheContentProvidersdocumentation.所以不建議用這種方式跟其他APP開放資料溝通瞜..
參考資料:http://developer.android.com/guide/topics/data/data-storage.htmlhttp://developer.android.com/reference/android/content/Context.html#MODE_WORLD_WRITEABLE
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: