Android复制数据库从内部到外部存储 [英] Android copy database from internal to external storage

查看:923
本文介绍了Android复制数据库从内部到外部存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将我现有的数据库从我的内部到外部存储,但我有一个问题。它表示文件不存在。这里是我实际使用:



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

  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();
}
}

p>

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

但它不工作,我很确定内部存储中的数据库。 p>

如果我在 copyFile / sdcard / .Example / Data / 它正在 .Example 中创建文件数据



任何建议我缺少什么?

解决方案

如果你不知道你的应用程序路径,您可以使用:

  //没有资源泄漏:<未分配关闭值> ,这样的东西
public void copyAppDbToDownloadFolder()throws IOException {
try {
String currDate = Constants.APP_DATE_SHORT_FORMAT.format(new Date());
文件backupDB =新文件(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),DbContract.DATABASE_BUCKUP_NAME + currDate +.db);
文件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());
//或fis.getChannel()。transferTo(0,fis.getChannel()。size(),fos.getChannel());
fis.close();
fos.close();
Log.i(数据库成功,复制到下载文件夹);
return true;
}
} catch(IOException e){
Log.d(复制数据库,失败,原因:,e);
}
}

然后你可以使用这个:

  public void copyAppDbToDownloadFolder()throws IOException {
try {
File backupDB = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),toDatabaseName); //例如my_data_backup.db
文件currentDB = getApplicationContext()。getDatabasePath(databaseName); // databaseName =您当前的应用程序数据库名称,例如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());
//或fis.getChannel()。transferTo(0,fis.getChannel()。size(),fos.getChannel());
fis.close();
fos.close();
Log.i(数据库成功,复制到下载文件夹);
return true;
} else Log.i(复制数据库,失败,数据库未找到);
} catch(IOException e){
Log.d(复制数据库,失败,原因:,e);
}
}

这在我的Nexus 4设备上可以正常工作。 / p>

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?

解决方案

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.

这篇关于Android复制数据库从内部到外部存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆