[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之後讀取出來
這兩隻function,如果你傳入的路徑,是沒有檔案夾的,會自動幫忙建立寫入/讀取根/data/data/[packagename]/files/的部分:
結果:
用文件管理器檢視:
寫入至檔案夾中的:
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
就是我將資料寫入至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存取的屬性
參考資料:
相关文章推荐
- [Xamarin] 使用Webview 來做APP (转帖)
- [Xamarin] 關於發出Notification 的大小事 (转帖)
- Silverlight 4使用Domain Service做資料快速存取
- 【转】[ASP.NET]使用jQuery的Ajax存取資料(ashx,aspx,asmx)
- [Xamarin] 簡單使用AlertDialog (转帖)
- [Xamarin] 簡單使用Fragment 靜態篇 (转帖)
- [Xamarin] 使用LayoutInflater.Inflate載入預先設計好的Layout並使用 (转帖)
- [Xamarin.Android] 如何透過電子郵件部署Xamarin.Android App (转帖)
- [Xamarin] 關於SQLite 的操作 (转帖)
- [Xamarin] 開啟另外一個Activity 並且帶資料 (转帖)
- [Xamarin.Android] 如何使用Google Map V2 (转帖)
- [Xamarin] 透過WebClient跟網路取得資料 (转帖)
- [Xamarin.Android] 儲存資料於Windows Azure (转帖)
- 转】[ASP.NET]使用jQuery的Ajax存取資料(ashx,aspx,asmx)
- VS2015 Xamarin使用经验3-更新版本打包发布Android APP
- 在iOS10中使用Speech Framework创建一个语音转文字的应用App
- app包中的fragment和V4包中的fragment使用的区别
- [置顶] App首页常见底部Path按钮(一分钟学会使用)
- 使用appframework前端框架中输入框圆角问题
- Maven 使用 webapp archetype 创建Web 应用后无法找到 java 和 test 文件夹