0

我使用教程将数据库文件包含到我的 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();

}

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

4

2 回答 2

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

试试这个方法,它对我来说很好用..如果你觉得有用,请点击接受..

于 2012-05-03T05:37:10.197 回答
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();

}
于 2012-05-03T06:51:11.077 回答