Ich verwende diesen Code, um meine SQLite-Datenbank aus dem Verzeichnis "Assets" in Android-Telefon "/ data/data/my_packname/databases /" Verzeichnis zu kopieren.Problem mit der Verwendung meiner eigenen SQLite-Datenbank
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();
}
...
}
Aber in Linie
while ((length = myInput.read(buffer)) > 0){
ich habe IOException!
Kann mir jemand sagen, was ich falsch mache?
Sehen Sie, wenn Sie Ihre Formatierung korrigieren und zeigen, was Ihre database ist. Setzen Sie auch einen Unterbrechungspunkt in und sehen Sie, ob Ihr myimput die Datei geöffnet hat, die iylooks wie es nicht hat. Schlechte Schreibweise oder Pfad vielleicht. – Emile
Danke für die Antwort. Ich habe Pfad und Datenbanknamen überprüft, es ist in Ordnung. – Alex
Emile, kannst du mir erklären, wie ich sehen kann, dass mein Eingang geöffnet hat? – Alex