Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android copy database from internal to external storage

I'm trying to copy my existing databases from my Internal to External Storage,but I have a little problem. It says that the file does not exist. Here is what I'm using actually :

to copy files from internal to external storage :

    private void copyFile(String src, String dst) throws IOException{
    FileChannel inChannel = new FileInputStream(src).getChannel();
    FileChannel outChannel = new FileOutputStream(dst).getChannel();
    try
    {
        inChannel.transferTo(0, inChannel.size(), outChannel);
    }
    finally
    {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }
}

and I'm using it like this :

copyFile("/data/data/com.example.test/databases/database.sqlite","/sdcard/.Example/Data/database.sqlite");

but it's not working and I'm pretty sure that the database in internal storage is there.

If I set as a destination folder in copyFile "/sdcard/.Example/Data/" it's creating file Data in .Example.

Any suggestions what I'm missing?

like image 446
Android-Droid Avatar asked Dec 07 '22 16:12

Android-Droid


2 Answers

Use this to copy your database to sdcard.

public void copyFile()
    {
        try 
        {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();

            if (sd.canWrite()) 
            {
                String currentDBPath = "\\data\\your.package.name\\databases\\dabase_name";
                String backupDBPath = "database_name";
                File currentDB = new File(data, currentDBPath);
                File backupDB = new File(sd, backupDBPath);

                if (currentDB.exists()) {
                    FileChannel src = new FileInputStream(currentDB).getChannel();
                    FileChannel dst = new FileOutputStream(backupDB).getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                }
                if(bool == true)
                {
                    Toast.makeText(Settings.this, "Backup Complete", Toast.LENGTH_SHORT).show();
                    bool = false;
                }
            }               
        } 
        catch (Exception e) {
            Log.w("Settings Backup", e);
        }
    }
like image 104
Deepak Avatar answered Dec 23 '22 18:12

Deepak


If you don't know your application path then you can use this:

//for without "Resource leak: '<unassigned Closeable value>' is never closed" warning, something like this
public void copyAppDbToDownloadFolder() throws IOException {
    try {
        String currDate = Constants.APP_DATE_SHORT_FORMAT.format(new Date());
        File backupDB = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), DbContract.DATABASE_BUCKUP_NAME + currDate + ".db");
        File currentDB = appCon.getDatabasePath(DbContract.DATABASE_NAME);
        if (currentDB.exists()) {
            FileInputStream fis = new FileInputStream(currentDB);
            FileOutputStream fos = new FileOutputStream(backupDB);
            fos.getChannel().transferFrom(fis.getChannel(), 0, fis.getChannel().size());
            // or fis.getChannel().transferTo(0, fis.getChannel().size(), fos.getChannel());
            fis.close();
            fos.close();
            Log.i("Database successfully", " copied to download folder");
            return true;
        }
    } catch (IOException e) {
        Log.d("Copying Database", "fail, reason:", e);
    }
}

Or if you need copy database to public "Download" folder then you can use this:

public void copyAppDbToDownloadFolder() throws IOException {
    try {
        File backupDB = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "toDatabaseName"); // for example "my_data_backup.db"
        File currentDB = getApplicationContext().getDatabasePath("databaseName"); //databaseName=your current application database name, for example "my_data.db"
        if (currentDB.exists()) {
            FileInputStream fis = new FileInputStream(currentDB);
            FileOutputStream fos = new FileOutputStream(backupDB);
            fos.getChannel().transferFrom(fis.getChannel(), 0, fis.getChannel().size());
            // or fis.getChannel().transferTo(0, fis.getChannel().size(), fos.getChannel());
            fis.close();
            fos.close();
            Log.i("Database successfully", " copied to download folder");
            return true;
        } else Log.i("Copying Database", " fail, database not found");
    } catch (IOException e) {
        Log.d("Copying Database", "fail, reason:", e);
    }
}

This is working perfectly on my Nexus 4 device.

like image 38
SBotirov Avatar answered Dec 23 '22 17:12

SBotirov