有什么方法可以从应用程序内部将SQLite数据库文件从应用程序传输到PC?

时间:2018-10-09 18:40:17

标签: java android

我有一个将信息存储在表格中的应用程序,最终将其传输到PC。但是,我找不到有关如何将db文件从手机传输到PC的任何可靠信息。

1 个答案:

答案 0 :(得分:0)

只需添加此权限即可显示:

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

并像这样备份数据库:

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

        if (sd.canWrite()) 
        {
            String currentDBPath = "\\data\\your.package.name\\databases\\dabase_name"; /* REMEMBER TO CHANGE THIS */
            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);
    }
}

现在您可以将其(从存储设备)手动传输到PC或上传到服务器