0

I used this tutorial to include a database file to my android app. It works fine on my HTC Decire HD. I wanted to run it on emulator to see if tablet layouts look well. Unfortunately the app fails with an error.

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;

    //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){  <------ HERE, at first iteration
        myOutput.write(buffer, 0, length);
    }

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

}

The message for this error is just 'null', nothing more. Can this be fixed?

Andrzej Gis
  • 13,706
  • 14
  • 86
  • 130

2 Answers2

0
private void copyfromAsset()
{
  try {
            String FILE_TO_READ="data.txt"; //file in asset folder
            String TEMP_FILE_NAME="temp.txt"; //or whatever file name you want to give
            byte[] buffer = new byte[1024];
            int len1 = 0;


            InputStream istr=(con.getAssets().open(FILE_TO_READ));
            FileOutputStream fos=openFileOutput(TEMP_FILE_NAME,MODE_WORLD_READABLE);

            while ((len1 = istr.read(buffer)) !=-1) {
                fos.write(buffer, 0, len1); // Write In FileOutputStream.
            }
            fos.flush();
            fos.close();

            istr.close();

        }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

try this method it is working fine for me....hit accept if you found usefull..

Sandeep
  • 2,573
  • 3
  • 21
  • 28
  • It works fine for me too, but with database it's a little different. I have to put it in a specific directory to be able to use it later. – Andrzej Gis May 03 '12 at 14:37
0
public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();

    if (dbExist) {
        // do nothing - database already exist
    }
    else {

        // By calling this method and empty database will be created into
        // the default system path
        // of your application so we are going to be able to overwrite that
        // database with our database.

        try {

            copyDataBase();
        }
        catch (IOException e) {

            throw new Error("Error copying database");

        }
    }
}


/**
 * Check if the database already exist to avoid re-copying the file each
 * time you open the application.
 * 
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase() {

    File dbFile = new File(DB_PATH + DB_NAME);
    return dbFile.exists();
}


/**
 * Copies your database from your local assets-folder to the just created
 * empty database in the
 * system folder, from where it can be accessed and handled.
 * This is done by transferring byte-stream.
 */
private void copyDataBase() throws IOException {

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

    // Path to the just created empty DB
    String outFileName = DB_PATH + DB_NAME;

    // Open the empty DB as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    // transfer bytes from the input-file to the output-file
    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();

}
Krishnakant Dalal
  • 3,568
  • 7
  • 34
  • 62
  • It crashes in this line: OutputStream myOutput = new FileOutputStream(outFileName); outFileName is "/data/data/com.CopyFromAssets/databases/data.db" It looks like this path should be different in emulator.. – Andrzej Gis May 03 '12 at 14:35
  • you have to give db name and path (Static variables) Put some efforts to overcome the error this code is working properly tested – Krishnakant Dalal May 03 '12 at 16:11
  • Dude, have you even read the question? What you posted here is just what you can find in the link I provided in the question. Moreover I pointed out that the problem is related ONLY to the emulator. I app worked fine on my smartphone. – Andrzej Gis May 03 '12 at 16:50
  • I figured out that there was no ..../databases directory on emulator. I created it using abd. Than the error moved back to (length = myInput.read(buffer)) I suppose it may have something to do with emulator configuration. – Andrzej Gis May 03 '12 at 16:54