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



我使用本教程将数据库文件包含在我的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",仅此而已。这可以解决吗?

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

试试这种方法,它对我来说工作正常....点击接受,如果你发现有用..

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

最新更新