Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正在尝试将SQLite db保存到SD卡_Java_Android_Sqlite - Fatal编程技术网

Java 正在尝试将SQLite db保存到SD卡

Java 正在尝试将SQLite db保存到SD卡,java,android,sqlite,Java,Android,Sqlite,可能重复: 我正在尝试将sqlite文件保存到SD卡。我用的是这个代码 这是一个评价很高的答案,所以我认为它应该非常简单。我在logcat中没有错误。我没有看到任何已创建的目录/已创建的文件。我在清单中也有“向外部写入”权限 不要忘记在amnifest文件中声明权限 使用权限android:name=“android.permission.WRITE_EXTERNAL_STORAGE”文件sd=Environment.getExternalStorageDirectory(); File da

可能重复:

我正在尝试将sqlite文件保存到SD卡。我用的是这个代码


这是一个评价很高的答案,所以我认为它应该非常简单。我在logcat中没有错误。我没有看到任何已创建的目录/已创建的文件。我在清单中也有“向外部写入”权限

不要忘记在amnifest文件中声明权限


使用权限android:name=“android.permission.WRITE_EXTERNAL_STORAGE”

文件sd=Environment.getExternalStorageDirectory(); File data=Environment.getDataDirectory()


从记录异常开始,而不是简单地捕获异常<代码> catch(异常e){} /代码>当然,日志中没有任何东西。如果有东西在中间工作,它不会记录为一个错误吗?会有一个异常抛出,它会被你的catch块捕获,并且没有任何东西可以完成,因为你的catch块是空的。至少在里面放一个e.printStackTrace(),好吧,我输入了e.printStackTrace(),然后我运行了代码,结果什么都没有出现。另外,关于可能的重复,旧的问答让代码在eclipse中运行时没有错误。这个问题是为了帮助调试代码以使其实际运行,因为似乎有一些逻辑错误阻止它这样做。您不会得到logcat错误,因为您有一个空的
catch
块!请删除它。在我问题的底部,“我的清单中也有“写入外部”权限。”这段代码对我来说就像一个咒语,几乎是相同的,可能是sdcardGetting错误的路径:真的吗?这就是你的错误?我会把你标记为答案。我只需要将“.db”添加到我的原始输出文件中,现在它就可以正常工作了我告诉过你:p
 try {
    File sd = Environment.getExternalStorageDirectory();
    File data = Environment.getDataDirectory();

    if (sd.canWrite()) {
        String currentDBPath = "\\data\\com.test.mytest\\databases\\test_db";
        String backupDBPath = "test_db";
        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();
        }
    }
} catch (Exception e) {
}
         if (sd.canWrite()) {
             String currentDBPath = "/data/packagename/databases/DATABASENAME";
             String backupDBPath = "/backup/"+"main.db";
             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();
             } else {
                 Log.e(TAG, "File does not exist: " + currentDBPath);
             }