0

I was previously storing a sqlite database in my apps assets folder but have now moved the database to external storage.

My previous copyDatabase() method looked like this.

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

The issue is I'm unsure how to create an InputStream for opening the database from external storage. I can't seem to find the external storage equivalent to myContext.getAssets().open(DATABASE_NAME);

The current database path:

DB_PATH = Environment.getExternalStorageDirectory().getPath().toString()+"/SoulInfoDatabase/BB2SoulDatabase.db";
M0rty
  • 985
  • 3
  • 15
  • 35
  • You may wish to have a look at [SQLite database on SD card](http://stackoverflow.com/questions/4806181/sqlite-database-on-sd-card), as this may provide the solution. – MikeT Apr 18 '16 at 03:03
  • Possible duplicate of [Ship an application with a database](http://stackoverflow.com/questions/513084/ship-an-application-with-a-database) – Ali Asheer Dec 26 '16 at 07:59

1 Answers1

1

Step 1: Give storage permission in your App Manifesto file:

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

Step 2: Copy database to your custom SD card Path

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

Step 3: Then Open your database:

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";

  • Hi Shahadat, thanks for the reply. Wondering how does InputStream myInput = context.getAssets().open(DB_NAME); work when I don't have a database in my assets folder? – M0rty Apr 18 '16 at 06:50
  • 1
    Im wanting to write from external storage to internal storage – M0rty Apr 18 '16 at 06:52
  • Are you sure that you don't have any database sqlite file in assets folder? – Md. Shahadat Sarker Apr 18 '16 at 06:54
  • Yes I'm sure, what I'm doing is downloading a database to external storage, I then want to copy that database into internal storage so I can read it. – M0rty Apr 18 '16 at 06:56
  • I would like to be able to just read the database straight from the external storage but I can't do that. – M0rty Apr 18 '16 at 06:57
  • you can't copy a file into internal storage. But you can create new database into the android OS/System but not database file... – Md. Shahadat Sarker Apr 18 '16 at 07:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109401/discussion-between-m0rty-and-md-shahadat-sarker). – M0rty Apr 18 '16 at 07:01