2012-09-19 53 views
5

使用模擬器時,您的sqlite文件存儲在主應用程序文件夾附近的文件夾中,您可以下載它。但是這個功能在沒有固定設備的情況下無法訪問。我該如何以編程方式在SD卡中備份這個現有的sqlite文件如何以編程方式備份​​SD卡中的sqlite文件?

我想在我的應用程序中有一個按鈕,將此文件存儲在SD卡中的特殊路徑中。可能嗎?

感謝,

+0

類似的問題在這裏更換MY_DB_NAME http://stackoverflow.com/questions/1995320/how- to-backup-database-file-to-sdcard-on-android –

+0

它並沒有說你可以如何將現有的數據庫複製到SD卡 – breceivemail

回答

16

你可以試試這個工作對我來說,要記得拿到WRITE_EXTERNAL_STORAGE許可,您的清單:

// Copy to sdcard for debug use 
    public static void copyDatabase(Context c, String DATABASE_NAME) { 
     String databasePath = c.getDatabasePath(DATABASE_NAME).getPath(); 
     File f = new File(databasePath); 
     OutputStream myOutput = null; 
     InputStream myInput = null; 
     Log.d("testing", " testing db path " + databasePath); 
     Log.d("testing", " testing db exist " + f.exists()); 

     if (f.exists()) { 
      try { 

       File directory = new File("/mnt/sdcard/DB_DEBUG"); 
       if (!directory.exists()) 
        directory.mkdir(); 

       myOutput = new FileOutputStream(directory.getAbsolutePath() 
         + "/" + DATABASE_NAME); 
       myInput = new FileInputStream(databasePath); 

       byte[] buffer = new byte[1024]; 
       int length; 
       while ((length = myInput.read(buffer)) > 0) { 
        myOutput.write(buffer, 0, length); 
       } 

       myOutput.flush(); 
      } catch (Exception e) { 
      } finally { 
       try { 
        if (myOutput != null) { 
         myOutput.close(); 
         myOutput = null; 
        } 
        if (myInput != null) { 
         myInput.close(); 
         myInput = null; 
        } 
       } catch (Exception e) { 
       } 
      } 
     } 
    } 
1

你可以試試下面的代碼,

String path = Environment.getExternalStorageDirectory().toString() + "/path"; 
File folder = new File(path); 
if (!folder.exists()) 
{ 
    folder.mkdirs(); 
} 

File dbfile = new File(path + "/database.db"); 
if (!dbfile.exists()) 
{ 
    dbFile.createFile(); 
} 
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null); 
+0

這不**複製**現有的數據庫。它**在這條路徑上創建**。 – breceivemail

+0

@breceivemail根據您以前的問題標題**我怎樣才能以編程方式在SD卡中存儲sqlite文件?**,它正確滿足要求。 – Lucifer

1

你可以試試這個複製文件:

public void copyFile(File in, File out) { 
     String DialogTitel = getString(R.string.daten_wait_titel); 
     String DialogText = getString(R.string.kopiervorgang_laeuft); 
     try { 
      // Dialogdefinition Prograssbar 
      final ProgressDialog dialog = new ProgressDialog(this) { 
       @Override 
       public boolean onSearchRequested() { 
        return false; 
       } 
      }; 
      dialog.setCancelable(false); 
      dialog.setTitle(DialogTitel); 
      dialog.setIcon(R.drawable.icon); 
      dialog.setMessage(DialogText); 
      dialog.show(); 
      new Thread(new MyCopyThread(in, out)) { 
       @Override 
       public void run() { 
        try { 
         FileChannel inChannel = new FileInputStream(
           MyCopyThread.in).getChannel(); 
         FileChannel outChannel = new FileOutputStream(
           MyCopyThread.out).getChannel(); 
         try { 
          System.out.println("KOPIEREN"); 
          inChannel.transferTo(0, inChannel.size(), 
            outChannel); 
          if (inChannel != null) 
           inChannel.close(); 
          if (outChannel != null) 
           outChannel.close(); 
          setCopyError(false); 
         } catch (IOException e) { 
          setCopyError(true); 
          // throw e; 
         } finally { 
          if (inChannel != null) 
           inChannel.close(); 
          if (outChannel != null) 
           outChannel.close(); 
         } 
         dialog.dismiss(); 
         // Abschlussarbeiten 
         if (useExternalSD == true) { 
          // Externe DB 
          moveDBtoExternFinish(); 
         } else { 
          // Interne DB 
          moveDBtoInternFinish(); 
         } 
         moveDBFinishHandler.sendMessage(moveDBFinishHandler 
           .obtainMessage()); 
        } catch (Exception ex) { 

        } 
       } 
      }.start(); 
     } catch (Exception exx) { 

     } 

    } 

這是代碼讓你的內部數據庫的FILNAME:

File interneDB = getApplicationContext().getDatabasePath(MY_DB_NAME); 

與你的數據庫的名稱

相關問題