title: Copy database from assets folder
body: <p>I am creating a Dictionary application which uses an existing sqlite database. I have placed my database in assets folder and I am using the following code to copy the database when the app is started for first time. (I have borrowed the idea from <a href="http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/" rel="nofollow">this</a> post)</p>
  
  <p>DatabaseHelper.java</p>
  
  <pre><code>public class DatabaseHelper {
  
  private static String DB_PATH = "";
  private static String DB_NAME = "abc.sqlite";
  private SQLiteDatabase myDatabase;
  private Context myContext;
  
  public DatabaseHelper(Context context) {
  
      myContext = context;
  
      if (android.os.Build.VERSION.SDK_INT &gt;= 17)
          DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
      else
          DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
      Log.d("path", DB_PATH);
  }
  
  public void copyDatabase() {
  
          InputStream myInput;
          OutputStream outStream;
          try {
              myInput = myContext.getAssets().open(DB_NAME);
              String file = DB_PATH + DB_NAME;
              outStream = new FileOutputStream(file);
  
              byte[] buffer = new byte[1024];
              int length = 0;
              while ((length = myInput.read(buffer)) &gt;= 0) {
                  outStream.write(buffer, 0, length);             
              }
              outStream.flush();
              myInput.close();
              outStream.close();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
      }
  }
  </code></pre>
  
  <p>And then I call the above <code>copyDatabase()</code> method form my <code>MainActivity</code>. Here is the code.</p>
  
  <pre><code>public class MainActivity extends Activity {
      DatabaseHelper myDbHelper;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.layout);
          myDbHelper = new DatabaseHelper(getApplicationContext());
          myDbHelper.copyDatabase();
  
  
      }
  
  }
  </code></pre>
  
  <p>But the problem is that Android does not copy the database. I have used Log tool, and I found out that the while loop never runs. 
  What's the problem in this code. 
  Thanks.</p>