2014-05-15 3 views
0

Моя проблема заключается в том, что мое приложение всегда терпит неудачу, когда база данных копируется из папки актива на пути телефона:Загрузка базы данных из папки активов - FileOutputStream терпит неудачу

/data/data/at.atn.android/databases/ 

МОЙ имя_базы_данных:

atnRoutenplaner.sqlite3 

Мои код для трансфера:

private void copyDataBase() throws IOException{ 

    //Open your local db as the input stream 
    InputStream myInput = myContext.getAssets().open(DB_NAME); 

    // Path to the just created empty db 
    String outFileName = DB_PATH + DB_NAME; 
    File sampleFile = new File(outFileName); 
    //Open the empty db as the output stream 
    OutputStream myOutput = new FileOutputStream(outFileName); 

    //transfer bytes from the inputfile to the outputfile 
    byte[] buffer = new byte[1024]; 
    int length; 
    while ((length = myInput.read(buffer)) > 0) { 
     myOutput.write(buffer, 0, length); 
    } 

    //Close the streams 
    myOutput.flush(); 
    myOutput.close(); 
    myInput.close(); 

} 

ответ

0

попробовать это,

public void CopyDataBaseFromAsset() throws IOException{ 
     InputStream in = ctx.getAssets().open("mycontacts"); 
     Log.e("sample", "Starting copying"); 
     String outputFileName = DATABASE_PATH+DATABASE_NAME; 
     File databaseFile = new File("/data/data/com.copy.copydatabasefromasset/databases"); 
     // check if databases folder exists, if not create one and its subfolders 
     if (!databaseFile.exists()){ 
      databaseFile.mkdir(); 
     } 

     OutputStream out = new FileOutputStream(outputFileName); 

     byte[] buffer = new byte[1024]; 
     int length; 


     while ((length = in.read(buffer))>0){ 
       out.write(buffer,0,length); 
     } 
     Log.e("sample", "Completed"); 
     out.flush(); 
     out.close(); 
     in.close(); 

    } 
+0

жаль, что не сработало. Я не понимаю, почему. Я использовал бывшую базу данных с atnRoutenplaner.sqlite. Может, это конец? – user3552619

Смежные вопросы