Android从内部存储到外部存储的复制数据库

And*_*oid 2 database android

我正在尝试将现有数据库从内部存储复制到外部存储,但我有一点问题.它说the file does not exist.这是我实际使用的:

将文件从内部存储复制到外部存储:

    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();
    }
}
Run Code Online (Sandbox Code Playgroud)

而我正在使用它:

copyFile("/data/data/com.example.test/databases/database.sqlite","/sdcard/.Example/Data/database.sqlite");
Run Code Online (Sandbox Code Playgroud)

但它不起作用,我很确定内部存储中的数据库是否存在.

如果我在copyFile "/sdcard/.Example/Data/"其创建文件Data中设置为目标文件夹.Example.

有什么建议我缺少什么?

Dee*_*pak 7

使用此选项将数据库复制到SD卡.

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);
        }
    }
Run Code Online (Sandbox Code Playgroud)


mr.*_*fox 5

如果你不知道你的应用程序路径,那么你可以使用这个:

//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);
    }
}
Run Code Online (Sandbox Code Playgroud)

或者,如果您需要将数据库复制到公共“下载”文件夹,那么您可以使用:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这在我的 Nexus 4 设备上运行良好。


jac*_*nad 0

您是否添加了 WRITE_EXTERNAL_STORAGE 权限?

<?xml version="1.0" encoding="utf-8"?>
<manifest ...
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest> 
Run Code Online (Sandbox Code Playgroud)