Skip to content

Instantly share code, notes, and snippets.

@liwenwei
Created June 8, 2018 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liwenwei/48cd2de215d34aad4f8b09df7a3338a1 to your computer and use it in GitHub Desktop.
Save liwenwei/48cd2de215d34aad4f8b09df7a3338a1 to your computer and use it in GitHub Desktop.
How to import external database in Android?
package XXX;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;
import xxx.R;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by liwenwei on 6/8/2018.
*/
public class DBManager {
private final int BUFFER_SIZE = 400000;
private String DB_PATH;
private final String DB_NAME = "countries.db";
private SQLiteDatabase database;
private Context context;
public DBManager(Context context) {
this.context = context;
DB_PATH = "/data"
+ Environment.getDataDirectory().getAbsolutePath() + "/"
+ context.getPackageName();
}
public void open() {
database = open(DB_PATH + "/" + DB_NAME);
}
private SQLiteDatabase open(String dbFile) {
File file = new File(dbFile);
if (!file.exists()) {
try {
InputStream is = context.getResources().openRawResource(R.raw.countries);
FileOutputStream fos = new FileOutputStream(dbFile);
byte[] buffer = new byte[BUFFER_SIZE];
int len;
while ((len = is.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
is.close();
fos.close();
} catch (FileNotFoundException e) {
Log.e("Database", "File not found");
e.printStackTrace();
} catch (IOException e) {
Log.e("Database", "IO exception");
e.printStackTrace();
}
}
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
return db;
}
public void close() {
if (this.database != null) {
this.database.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment