2016-11-04 184 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