2014-03-06 22 views
-1

我需要我的數據庫從資產文件夾複製到「/data/data/com.example.provadatabase/databases/」不要硬編碼「/ data /」;使用Context.getFilesDir()的getPath(),而不是

我就是用這個代碼

private static String DB_PATH = "/data/data/com.example.provadatabase/databases/"; 

private static String DB_NAME = "MN1"; 

,然後創建一個數據庫,如果沒有,那麼它應該存在複製到創建的數據庫從資產

InputStream myInput = myContext.getAssets().open(DB_NAME); 

String outFileName = DB_PATH + DB_NAME; 

OutputStream myOutput = new FileOutputStream(outFileName); 

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

myOutput.flush(); 
myOutput.close(); 
myInput.close(); 

} 

該數據庫以及創建,但它doesen't我的數據庫複製到數據庫文件夾和代碼數據庫給這個警告「不要硬編碼「/ data /」;使用Context.getFilesDir()的getPath(),而不是」

我應該用什麼來複制我的分貝?

+3

我不明白.''使用Context.getFilesDir()。getPath()'你還需要什麼? – njzk2

回答

1

究竟是什麼錯誤告訴你這樣做。

String outFileName = context.getDatabasePath(DatabaseHelper.DB_NAME).getParentFile() + DB_NAME; 

這假定情境是可用的,它通常在構造函數中。但老實說,我只是用SQLiteAssetHelper

+0

在這樣一個way DB不會放在'/ databases /'文件夾下 – nikis

+0

有一個更好的版本... – PearsonArtPhoto

+0

它仍然doesent複製數據庫文件夾中的數據庫 – user3293120

相關問題