2016-04-18 4 views
0

Ich habe zuvor eine SQLite-Datenbank in meinem App Assets-Ordner gespeichert, aber jetzt die Datenbank auf externen Speicher verschoben.Öffnen einer Datenbank von externem Speicher Android

Meine vorherige copyDatabase() Methode sah so aus.

private void copyDataBase() throws IOException { 
     InputStream myInput = myContext.getAssets().open(DATABASE_NAME); 
     OutputStream myOutput = new FileOutputStream(DB_PATH); 
     byte[] buffer = new byte[AccessibilityNodeInfoCompat.ACTION_NEXT_HTML_ELEMENT]; 
     while (true) { 
      int length = myInput.read(buffer); 
      if (length > 0) { 
       myOutput.write(buffer, 0, length); 
      } else { 
       myOutput.flush(); 
       myOutput.close(); 
       myInput.close(); 
       return; 
      } 
     } 
    } 

Das Problem ist, ich bin nicht sicher, wie ein InputStream zum Öffnen der Datenbank aus dem externen Speicher zu erstellen. Ich kann nicht scheinen, um die externen Speicher entspricht myContext.getAssets().open(DATABASE_NAME);

Der aktuelle Datenbankpfad zu finden:

DB_PATH = Environment.getExternalStorageDirectory().getPath().toString()+"/SoulInfoDatabase/BB2SoulDatabase.db"; 
+0

Sie können sich die [SQLite-Datenbank auf SD-Karte] (http://stackoverflow.com/questions/4806181/sqlite-database-on-sd-card) ansehen, da dies die Lösung bieten kann. – MikeT

+0

Mögliches Duplikat von [Eine Anwendung mit einer Datenbank versenden] (http://stackoverflow.com/questions/513084/ship-an-application-with-a-database) –

Antwort

1

Schritt 1: Gibt Sie Speicher Erlaubnis in Ihrer App-Manifest-Datei:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

Schritt 2 : Kopieren Sie die Datenbank auf Ihre benutzerdefinierte SD-Karte. Pfad

private void copyDataBase() throws IOException { 
    // Open your local db as the input stream 
    InputStream myInput = context.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 
    new File(outFileName).createNewFile(); 
    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(); 
} 

Schritt 3: Öffnen Sie dann Ihre Datenbank:

try { 
     db = SQLiteDatabase.openDatabase(DB_PATH + "/" + DB_NAME, null, 
       SQLiteDatabase.OPEN_READWRITE 
         | SQLiteDatabase.NO_LOCALIZED_COLLATORS); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
     return false; 
    } 

WHERE

String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/SoulInfoDatabase"; 
File file = new File(filePath); 
if(!file.exists()){ 
    file.mkdirs(); 
} 

DB_PATH = filePath; DB_NAME = "BB2SoulDatabase.sqlite";

+0

Hallo Shahadat, danke für die Antwort. Ich frage mich, wie InputStream myInput = context.getAssets(). Open (DB_NAME); arbeiten, wenn ich keine Datenbank in meinem Assets-Ordner habe? – M0rty

+0

Ich möchte von externem Speicher auf internen Speicher schreiben – M0rty

+0

Sind Sie sicher, dass Sie keine Datenbank-SQLite-Datei im Ordner "Assets" haben? –

Verwandte Themen