Android备份数据库没有结果

时间:2016-01-24 07:13:47

标签: android sqlite android-sqlite database-backups

我正在尝试使用sqlite在我的Android应用程序中备份我的数据库。因为这是谷歌中的给定代码,但它在logcat中没有显示任何结果甚至错误。它应该是什么意思?对不起,我刚刚开始使用android dev ..

            try {

                File sd =  Environment.getDataDirectory();
                File data = Environment.getDataDirectory();
                FileChannel source=null;
                FileChannel destination=null;
                if (sd.canWrite()) {
                    String currentDBPath = "//data//" + "com.example.jes.myapplication"
                            + "//databases//" + "TINDERO.DB";
                    String backupDBPath = "//data//" + "com.example.jes.myapplication"
                            + "//databases//" + "//TINDERO_BACKUP//" + "TINDERO.DB";
                    File currentDB = new File(data, currentDBPath);
                    File backupDB = new File(sd, backupDBPath);

                    FileChannel src = new FileInputStream(currentDB).getChannel();
                    FileChannel dst = new FileOutputStream(backupDB).getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                    Toast.makeText(getApplicationContext(), "Backup Successful!",
                            Toast.LENGTH_SHORT).show();
                    Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();

                }
            } catch (IOException e) {
            Toast.makeText(getApplicationContext(), "Failed",Toast.LENGTH_SHORT).show();
            }


        }
    });

1 个答案:

答案 0 :(得分:0)

您的if (sd.canWrite())可能总是错误的。在块内放置一个断点,看它是否进入if块。或者放一个else块并检查。