Import SQLite Database from SDCARD to Android – No Assets folder

I am trying to import my sqlite database from sdcard or any external location to my Android application. My application requires database import so that the database schema will not change, but the records will be based on The database to be imported is changed.

(For example, I may import DatabaseA with 10 records at a given time, and another time I may import DatabaseA with 25 records. Database A always Import from the same external location).

The import method I have seen using the assets folder so far does not help. I want to import a database pointing to an external location.

I use the following code to import my database from sdcard.

Please note: You need to create one in the application The database folder to successfully import the database.

public void importDB() {

String dir=Environment.getExternalStorageDirectory().getAbsolutePath();
File sd = new File(dir);
File data = Environment.getDataDirectory();
FileChannel source = null;
FileChannel destination = null;
String backupDBPath = "/data/com.example.mine.move/databases/A.db";
String currentDBPath = "A.db";
File currentDB = new File(sd, currentDBPath);
File backupDB = new File(data, backupDBPath);
try {
source = new FileInputStream(currentDB).getChannel();
destination = new FileOutputStrea m(backupDB).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
Toast.makeText(this, "Please wait", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}

In addition, add the following permissions.

If The import is unsuccessful, please downgrade the SDK version or include runtime permissions.

I am trying to import my sqlite database from the sdcard or any external location to my Android application. The application requires database import so that the database schema will not change, but the records will change according to the database to be imported.

(For example, I might import a database with 10 records at a given time DatabaseA, and another time I might import DatabaseA with 25 records. Database A is always imported from the same external location).

The import method I have seen so far using the assets folder is not helpful. I think Import a database pointing to an external location.

I use the following code to import my database from sdcard.

Please note: required Create a database folder in the application to successfully import the database.

public void importDB() {

String dir=Environment.getExternalStorageDirectory( ).getAbsolutePath();
File sd = new File(dir);
File data = Environment.getDataDirectory();
FileChannel source = nu ll;
FileChannel destination = null;
String backupDBPath = "/data/com.example.mine.move/databases/A.db";
String currentDBPath = "A.db";
File currentDB = new File(sd, currentDBPath);
File backupDB = new File(data, backupDBPath);
try {
source = new FileInputStream(currentDB).getChannel( );
destination = new FileOutputStream(backupDB).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
Toast.makeText(this, "Please wait", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace( );
}

In addition, add the following permissions.

If the import is not successful, please downgrade the SDK version or include the runtime permissions.

Leave a Comment

Your email address will not be published.