0
このコードを使用して、sqliteデータベースを "Assets"ディレクトリから "/ data/data/my_packname/databases /"ディレクトリにコピーします。自分のsqliteデータベースを使用したときの問題
public class DataBaseHelper extends SQLiteOpenHelper{
private SQLiteDatabase myDataBase;
private final Context myContext;
public DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.myContext = context;
}
...
private void copyDataBase() throws IOException{
//Open your local db as the input stream
AssetManager am = this.myContext.getAssets();
InputStream myInput = am.open(DATABASE_NAME);
// Path to the just created empty db
String outFileName = DATABASE_PATH + DATABASE_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){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
...
}
しかしライン
while ((length = myInput.read(buffer)) > 0){
に私はにIOExceptionを持っています!
誰でも私が間違っていることを教えてもらえますか?
あなたの書式設定を修正し、あなたのデータベース名が何であるかを示すことができるかどうかを確認してください。また、ブレークポイントを入れて、あなたのmyimputがiylooksが持っていないようなファイルを開いているかどうか確認してください。おそらく誤ったスペルやパス。 – Emile
返信ありがとうございます。私はパスとデータベース名を確認しましたが、大丈夫です。 – Alex
Emile、myInputがどのように開いているかわかりますか? – Alex