2011-08-10 50 views
1

在Visual Studio和一個模擬器中使用MonoDroid,我試圖從資產文件夾中將資產「db.sqlite」複製到SD卡,以便我可以打開數據庫進行讀取/寫。MonoDroid:將資產複製到SD卡

當我運行該應用程序,它會死。 MonoDroid不給我任何調試信息。

string destPath = Path.Combine(Environment.GetFolderPath(
     Environment.SpecialFolder.Personal), "db.sqlite"); 

if (!File.Exists(destPath)) 
using (Stream stream = Assets.Open("db.sqlite")) 
{ 
    stream.CopyTo(File.Create(destPath)); 
    stream.Close(); 
} 

回答

2
+0

有沒有辦法來過濾輸出,只看到我Log.Debug()了。有一噸的信息流動,我錯了程序輸出 –

+1

DDMS是一個SDK附帶的工具,它提供了一系列很好的調試工具,包括logcat輸出,並且可以輕鬆過濾:http:// developer。 android.com/guide/developing/debugging/ddms.html –

2

嘗試保存與數據庫文件.mp3擴展名的文件,然後使用下面的腳本將它複製到SD卡

private void copyFilesToSdCard() { 
     copyFileOrDir(""); // copy all files in assets folder in my project 
    } 

    private void copyFileOrDir(String path) { 
     AssetManager assetManager = this.getAssets(); 
     String assets[] = null; 
     try { 
      Log.i("tag", "copyFileOrDir() "+path); 
      assets = assetManager.list(path); 
      if (assets.length == 0) { 
       copyFile(path); 
      } else { 
       String fullPath = TARGET_BASE_PATH + path; 
       Log.i("tag", "path="+fullPath); 
       File dir = new File(fullPath); 
       if (!dir.exists() && !path.startsWith("images") && !path.startsWith("sounds") && !path.startsWith("webkit")) 
        if (!dir.mkdirs()); 
         Log.i("tag", "could not create dir "+fullPath); 
       for (int i = 0; i < assets.length; ++i) { 
        String p; 
        if (path.equals("")) 
         p = ""; 
        else 
         p = path + "/"; 

        if (!path.startsWith("images") && !path.startsWith("sounds") && !path.startsWith("webkit")) 
         copyFileOrDir(p + assets[i]); 
       } 
      } 
     } catch (IOException ex) { 
      Log.e("tag", "I/O Exception", ex); 
     } 
    } 

    private void copyFile(String filename) { 
     AssetManager assetManager = this.getAssets(); 

     InputStream in = null; 
     OutputStream out = null; 
     String newFileName = null; 
     try { 
      Log.i("tag", "copyFile() "+filename); 
      in = assetManager.open(filename); 
      if (filename.endsWith(".mp3")) // extension was added to avoid compression on APK file 
       newFileName = TARGET_BASE_PATH + filename.substring(0, filename.length()-4); 
      else 
       newFileName = TARGET_BASE_PATH + filename; 
      out = new FileOutputStream(newFileName); 

      byte[] buffer = new byte[1024]; 
      int read; 
      while ((read = in.read(buffer)) != -1) { 
       out.write(buffer, 0, read); 
      } 
      in.close(); 
      in = null; 
      out.flush(); 
      out.close(); 
      out = null; 
     } catch (Exception e) { 
      Log.e("tag", "Exception in copyFile() of "+newFileName); 
      Log.e("tag", "Exception in copyFile() "+e.toString()); 
     } 
0

代碼爲我工作(它遞歸地複製文件或目錄和所有子目錄):

private void copy(string SourcePath){ 
     string[] list = Assets.List(SourcePath); 
     if (list.Length > 0) { 
      Directory.CreateDirectory(AndroidResorces.GetDatabaseStorage() + "/" + SourcePath); 
      foreach (string dirPath in Assets.List(SourcePath)){ 
       string newPath = SourcePath + "/" + dirPath; 
       copy(newPath); 
      } 
     } 
     else{ 
      Assets.Open(SourcePath).CopyTo(new FileStream(AndroidResorces.GetDatabaseStorage() + "/" + SourcePath, FileMode.OpenOrCreate)); 
     } 
    } 

用法:copy(path_relative_to_assets_root);

相關問題