2016-11-04 186 views
1

我一直在寻找近一天,这件事让我发疯。在android上找不到路径

我想打开一个连接到SQLite数据库我从Windows推到设备。

数据库现在是这个路径里面:

电话/安卓/数据/ MyApp.Droid /文件/ MyDB.db3

可以在不 '电话/ Android的' 我读认为。

当我尝试建立到数据库的连接时,我似乎无法找到路径。

这是我的代码,用于检索路径并创建连接。

public SQLite.SQLiteConnection GetConnection() 
{ 
    var sqliteFile = "MyDB.db3"; 
    var dataPath = global::Android.OS.Environment.DataDirectory.AbsolutePath; 
    var dataPathExtension = @"MyApp.Droid/files"; 

    var destination = Path.Combine(dataPath, dataPathExtension, sqliteFile); 

    //this outputs the following: /data/MyApp.Droid/files/MyDB.db3 
    //When I check my phone this is exactly when I can find the file. 

    return new SQLite.SQliteConnection(destination); 
    //It can't find the path/file. 
} 

数据库需要位于我可以从Windows访问的位置,而不需要设备生根。

任何人都可以向我解释为什么它找不到路径/文件,如果可能的话告诉我如何读取位置?

注:我不能从Windows访问任何 'Environment.SpecialFolder' 它似乎这给了我像一个路径:数据/用户/ 0/MyApp.Droid /文件/

此致,

+0

Android以许多奇怪的方式存储文件。你应该看看“Android解析文件路径”或类似的东西。根据你的Android版本,文件类型等,这将是不同的。 或者你是幸运的,只是忘记将它存储在ExternalStorage并检查权限(可以是内部的,外部并不意味着SD卡,因为... Google不关心逻辑) –

+0

您将不得不数据库文件在你的应用程序的沙箱外,下载dir,sdcard等。http://stackoverflow.com/questions/5858107/how-to-get-file-path-from-sd-card-in-android – SushiHangover

回答

1

我使用这个实现:

public SQLiteAsyncConnection GetConnection() 
    { 
     var fileName = "DatabaseName.db3"; 
     var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 
     var path = Path.Combine(documentsPath, fileName); 

     var connection = new SQLiteAsyncConnection(path); 

     return connection; 
    } 

然后,我会抓住用Android Device MonitorFile Explorer.db3文件。然后,您可以使用http://sqlitebrowser.org/或支持SQLite的其他浏览器打开.db3文件。 请注意:在Emulator上最容易,因为从物理设备中拉取文件可能非常繁琐,没有根。

  1. 打开Tools -> Android -> Android Device Monitor

enter image description here

  • Debug模式运行应用程序
  • Devices部选择您的设备:
  • enter image description here

  • 经由Save Icondata/data/com.mypackage.name/files拉你.db3文件:
  • enter image description here

  • 打开.db3文件中SQLite浏览器
  • enter image description here