0

I use this code to copy my sqlite database from "Assets" directory to Android-phone "/data/data/my_packname/databases/" directory.

public class DataBaseHelper extends SQLiteOpenHelper{   

    private SQLiteDatabase myDataBase; 

    private final Context myContext;

    public DataBaseHelper(Context context) {

     super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.myContext = context;
    } 

  ...

    private void copyDataBase() throws IOException{

     //Open your local db as the input stream

     AssetManager am = this.myContext.getAssets();

     InputStream myInput = am.open(DATABASE_NAME);


     // Path to the just created empty db
     String outFileName = DATABASE_PATH + DATABASE_NAME;

     //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();

    }

       ...
}

But in line

 while ((length = myInput.read(buffer)) > 0){

i have IOException!

Can anyone tell me what i'm doing wrong?

Alex
  • 1
  • See if you can correct your formatting and show what your databasename is. Also put a break point in and see if your myimput has opened the file which iylooks like it hasn't. Bad spelling or path perhaps. – Emile Jan 13 '11 at 14:18
  • Thanks for reply. I checked path and database name, it's ok. – Alex Jan 13 '11 at 15:19
  • Emile, can you explaine me how i can see myInput has opened? – Alex Jan 13 '11 at 15:23

1 Answers1

0

Try the code of answer of this link. the changes which I have done to the original code are commented.So it'll be easier for you to find what you are doing wrong.

Database not copying from assets

Community
  • 1
  • 1
Jaydeep Khamar
  • 5,975
  • 3
  • 32
  • 30