2011-08-10 9 views
1

En utilisant MonoDroid dans Visual Studio et un émulateur, j'essaie de copier un élément "db.sqlite" du dossier assets vers la carte SD afin de pouvoir ouvrir la base de données pour lire /écrire.MonoDroid: Copie d'un actif sur une carte SD

Lorsque je lance l'application, elle meurt. MonoDroid ne me donne aucune information de débogage.

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(); 
} 

Répondre

2
+0

est-il un moyen de filtrer la sortie pour ne voir que ce que je log.debug() sur. Il y a une tonne d'informations qui coule et je mets la sortie du programme –

+1

DDMS est un outil qui est livré avec le SDK qui vous donne un tas de bons outils de débogage, y compris la sortie logcat avec la possibilité de le filtrer facilement: http: // developer. android.com/guide/developing/debugging/ddms.html –

2

Essayez d'enregistrer le fichier avec le fichier de base de données extension .mp3, puis utiliser le script suivant pour le copier sur la carte 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

Code qui a fonctionné pour moi (il copie récursivement le fichier ou le répertoire et tous les sous-répertoires):

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)); 
     } 
    } 

Utilisation: copy(path_relative_to_assets_root);