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

}

このエラーのメッセージは単に「null」であり、それ以上のものではありません。これは修正できますか?

4

2 に答える 2

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 に答える
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 に答える