Download SQLite file programmatically

rav :

In my android application i'm using SQLite database. And I need to have an option in the app to download the database file.

Because sometimes I need to view the data there , so the user will download the file and send it to me and i will browse it using SQLite browsers.

Is the doable? if yes how?

   String src = "/data/data/myPackage/databases/Mydb.db";
   String dest = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();

copyFileOrDirectory(src , dest);

 public static void copyFileOrDirectory(String srcDir, String dstDir) {

        try {
            File src = new File(srcDir);
            File dst = new File(dstDir, src.getName());

                copyFile(src, dst);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void copyFile(File sourceFile, File destFile) throws IOException {
        if (!destFile.getParentFile().exists())
            destFile.getParentFile().mkdirs();

        if (!destFile.exists()) {
            destFile.createNewFile();
        }

        FileChannel source = null;
        FileChannel destination = null;

        try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = new FileOutputStream(destFile).getChannel();
            destination.transferFrom(source, 0, source.size());
        } finally {
            if (source != null) {
                source.close();
            }
            if (destination != null) {
                destination.close();
            }
        }
    }
Nizar :

Usually, the SQLite database is available in the /data/data/your.applications.package/databases/database.db path.

So, you could use that path; however, I suggest that you get the Database Path in the following way:

File dbFile = getDatabasePath("dbname");
String dbPath = dbFile.getPath();

Then, you can copy the database from this folder into the folder that you desire. Copying the database to the Downloads could simulate the "download" of the SQLite database.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=359103&siteId=1