我正在尝试在Xamarin Forms应用程序中使用LiteDB,但是我似乎无法访问该文件的写入或读取。
从我的MainViewModel.cs
调用我已将权限添加到AndroidManifest.xml;
<uses-sdk android:minSdkVersion="23" android:targetSdkVersion="25" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_USER_DICTIONARY" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.READ_SOCIAL_STREAM" />
<uses-permission android:name="android.permission.READ_SYNC_STATS" />
<uses-permission android:name="android.permission.REBOOT" />
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.SET_ORIENTATION" />
<uses-permission android:name="android.permission.SET_DEBUG_APP" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_PROFILE" />
<uses-permission android:name="android.permission.WRITE_GSERVICES" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
我在Android 6.0中添加了应用程序的权限;
更新:我检查了路径访问权限,可以写出文件。
string documentsPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
var dirPath = documentsPath + "/MyTestFolder";
var exists = Directory.Exists(dirPath);
var filepath = dirPath + "/test.txt";
if (!exists)
{
Directory.CreateDirectory(dirPath);
if (!System.IO.File.Exists(filepath))
{
var newfile = new Java.IO.File(dirPath, "test.txt");
using (FileOutputStream outfile = new FileOutputStream(newfile))
{
string line = "The very first line!";
outfile.Write(System.Text.Encoding.ASCII.GetBytes(line));
outfile.Close();
}
}
}
然而,这段代码失败了;
string documentsPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
var dirPath = documentsPath + "/MyData/MyDB.db";
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
using (var db = new LiteDatabase(dirPath))
{
// Get customer collection
var collection = db.GetCollection<T>(typeof(T).Name);
collection.Insert(tobj);
return true;
}
答案 0 :(得分:2)
您需要在Android中使用以下代码来访问存储/访问您的文件:
var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var dirPath = Path.Combine(documentsPath, "MyDB.db");
答案 1 :(得分:0)
如果我使用存储库模式,一切都很好。
using (var db = new LiteRepository("/storage/emulated/0/MyData/MyDB.db"))
{
db.Insert(new TestClass {Id = 1});
}
LiteDB存储库模式 https://github.com/mbdavid/LiteDB/wiki/LiteRepository