在模拟器上复制数据库表单资产失败

时间:2012-05-03 00:37:51

标签: android sqlite

我使用this教程将数据库文件包含到我的Android应用程序中。它适用于我的HTC Decire HD。我想在模拟器上运行它以查看平板电脑布局是否看起来很好。不幸的是,该应用失败并出现错误。

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

}

此错误的消息只是'null',仅此而已。这可以解决吗?

2 个答案:

答案 0 :(得分: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();
    }
}

尝试这种方法它对我来说很好....如果你发现有用的话就点击接受..

答案 1 :(得分: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();

}