2014-09-11 86 views

回答

1

貴medDB有任何延期?如果是這樣,把它像assetManager.open(「medDB.sqlite」); 您的文件夾結構告訴相同。 所以應該像

AssetManager assetManager = getAssets(); 
InputStream myInput = assetManager.open("medDB.sqlite"); 

我在項目中使用此代碼:

private void copyFile() { 
     AssetManager assetManager = mcontext.getAssets(); 
     File f1 = new File(SDCARD_Outer_PATH); 
     f1.mkdirs(); 
     InputStream in = null; 
     OutputStream out = null; 
     try { 
      in = assetManager.open("sample.sqlite"); 
      out = new FileOutputStream(SDCARD_DB_PATH); 

      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", e.getMessage()); 
     } 

    } 
+0

結果是一樣的。我認爲medDB.sqlite可能以某種方式附加到項目中。但我不知道如何 – 2014-09-11 11:24:35

+0

通常,在第一次使用時,我們將資產db複製到SD汽車並從那裏使用它。不知道你是如何設計的。此代碼適用於我。 – Jithu 2014-09-11 11:26:15

+0

手動複製?我試圖用代碼來做到這一點,但我需要從資產文件中獲取流。我手動將.sqlite數據庫放置在assets文件夾中。這是錯誤的方式? – 2014-09-11 11:28:42