2013-12-23 68 views
0

我需要你的幫助,我完成了一個應用程序的編碼,但問題在於數據庫在系統中:/data/data/your.package.name,我試圖通過這個數據庫訪問SD卡,我已經看在這個論壇的解決方案,也有很多,但我不知道如何使它工作,例如:將數據庫導出到SD卡

  private class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> { 
    private final ProgressDialog dialog = new ProgressDialog(ctx); 

    // can use UI thread here 
    protected void onPreExecute() { 
     this.dialog.setMessage("Exporting database..."); 
     this.dialog.show(); 
    } 

    // automatically done on worker thread (separate from UI thread) 
    protected Boolean doInBackground(final String... args) { 

     File dbFile = 
       new File(Environment.getDataDirectory() + "/data/com.mypkg/databases/mydbfile.db"); 

     File exportDir = new File(Environment.getExternalStorageDirectory(), ""); 
     if (!exportDir.exists()) { 
      exportDir.mkdirs(); 
     } 
     File file = new File(exportDir, dbFile.getName()); 

     try { 
      file.createNewFile(); 
      this.copyFile(dbFile, file); 
      return true; 
     } catch (IOException e) { 
      Log.e("mypck", e.getMessage(), e); 
      return false; 
     } 
    } 

    // can use UI thread here 
    protected void onPostExecute(final Boolean success) { 
     if (this.dialog.isShowing()) { 
      this.dialog.dismiss(); 
     } 
     if (success) { 
      Toast.makeText(ctx, "Export successful!", Toast.LENGTH_SHORT).show(); 
     } else { 
      Toast.makeText(ctx, "Export failed", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    void copyFile(File src, File dst) throws IOException { 
     FileChannel inChannel = new FileInputStream(src).getChannel(); 
     FileChannel outChannel = new FileOutputStream(dst).getChannel(); 
     try { 
      inChannel.transferTo(0, inChannel.size(), outChannel); 
     } finally { 
      if (inChannel != null) 
      inChannel.close(); 
      if (outChannel != null) 
      outChannel.close(); 
     } 
    } 

} 
+0

這是我應該添加給其他人的類嗎?以及如何執行? –

+1

將它作爲活動課內的私人課程。創建一個實例:ExportDatabaseFileTask task = new ExportDatabaseFileTask()。執行它:task.execute。 – NigelK

+0

非常感謝回覆,whish課程?主要的 ?或數據庫適配器? –

回答

2

這可能對您有所幫助。

File f=new File("/data/data/YOURAPPPACKAGENAME/databases/YOURDATABASENAME"); 
         FileInputStream fis=null; 
        FileOutputStream fos=null; 

        try 
        { 
         fis=new FileInputStream(f); 
         fos=new FileOutputStream("YOURSDCARDFOLDERPATH"+"/YOUREXPORTDDATBASENAME"); 
         while(true) 
         { 
         int i=fis.read(); 
         if(i!=-1) 
         {fos.write(i);} 
         else 
         {break;} 
         } 
         fos.flush(); 
         Toast.makeText(ctx, "DB dump OK", Toast.LENGTH_LONG).show(); 
        } 
        catch(Exception e) 
        { 
         e.printStackTrace(); 
         Toast.makeText(ctx, "DB dump ERROR", Toast.LENGTH_LONG).show(); 
        } 
        finally 
        { 
         try 
         { 
         fos.close(); 
         fis.close(); 
         } 
         catch(Exception ioe) 
         {} 
        } 

您必須添加SD卡讀取和寫入權限到您的manifest.xml文件和數據庫成功出口可以拉動使用DDMS工具到您的計算機這個數據庫,開放使用SQLite Manager後,。

+0

正如我之前告訴你的問題不是關於代碼,它是無處不在,但如何使用它,例如在哪裏把這個代碼放在哪個活動,這是我的問題。感謝你的努力的朋友:) –

+1

你只是直接把這個代碼放到你的按鈕點擊列表事件onclick()和/或如果你想實現這個到AsyncTask那麼你可以把這個代碼放入InBackground()。這很容易。 –

+0

我非常感謝你的貢獻,我認爲這比這更復雜,謝謝。 –