且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

安卓:数据库导出工作在模拟器,而不是手机

更新时间:2023-02-27 07:47:50

您提供文件错误的参数。从Android的文档:

You are supplying File wrong parameters. From Android doc:

File(String parent, String child) 
    parent - The parent pathname string
    child - The child pathname string 

在您的code:

parent => \\data  
child => \\data\\com.mypck.myapp\\databases\\database  
resulting path => \\data\\data\\com.mypck.myapp\\databases\\database => wrong

这可能是这样的:

It could be like this:

String sd = Environment.getExternalStorageDirectory();
if (sd.canWrite()) {
    String currentDBPath = "data/data/com.mypck.myapp/databases/your_database_name.db";
    String backupDBPath = sd + "/your_output_file_name.db";
    File currentDB = new File(currentDBPath);
    File backupDB = new File(backupDBPath);
    /* ... */

我很惊讶这个工作在模拟器,我对此表示怀疑。它可能会创建一个空文件,原因至少这行做某种感觉,虽然你的变量是不是你叫他们:

It surprises me this works on emulator, I doubt that. It probably creates an empty file becuase at least this line makes some sort of sense, although your variables are not what you named them:

File backupDB = new File(sd, backupDBPath);

另外,你或许应该创建一个最后块,并做有一些 FileChannel 清理。