Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.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
Android:数据库导出在emulator上工作,而不是在手机上_Android_Database_Export - Fatal编程技术网

Android:数据库导出在emulator上工作,而不是在手机上

Android:数据库导出在emulator上工作,而不是在手机上,android,database,export,Android,Database,Export,我正在尝试将应用程序的数据库导出到SD卡。它的工作非常适合模拟器,但不适合我的手机 OnClickListener mExportListener = new OnClickListener(){ public void onClick(View v){ try { File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirector

我正在尝试将应用程序的数据库导出到SD卡。它的工作非常适合模拟器,但不适合我的手机

    OnClickListener mExportListener = new OnClickListener(){
public void onClick(View v){
    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();
        if (sd.canWrite()) {
            String currentDBPath = "\\data\\com.mypck.myapp\\databases\\database";
            String backupDBPath = "database.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{
             String msg = activity.getResources().getString(R.string.something_wrong);        
             Toast toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG);  
             toast.show();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
};
这个
currentDB.exists()
在手机上返回false,但我检查了文件-它存在。
我的手机怎么了?

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

File(String parent, String child) 
    parent - The parent pathname string
    child - The child pathname string 
在代码中:

parent => \\data  
child => \\data\\com.mypck.myapp\\databases\\database  
resulting path => \\data\\data\\com.mypck.myapp\\databases\\database => wrong
可能是这样的:

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);
    /* ... */
这在emulator上工作让我感到惊讶,我对此表示怀疑。它可能会创建一个空文件,因为至少这一行有某种意义,尽管变量不是您所命名的:

File backupDB = new File(sd, backupDBPath);

此外,您可能还应该创建一个
finally
块,并在其中执行一些
文件通道
清理。

对不起,恐怕我没有正确理解您的评论。您建议我更改字符串currentDBPath=“\\data\\com.mypck.myapp\\databases\\database”;使用字符串currentDBPath=“/data/data/com.mypck.myapp/database”??是的,使用路径字符串currentDBPath=“/data/data/com.mypck.myapp/databases/database.db”并让我知道发生了什么。我不知道它是如何发生的=)无论如何,拉斯维加斯的以下答案与您的答案类似,两个答案都是正确的。好的,请接受拉斯维加斯的答案,点击下面投票符号左侧的右标记。不幸的是,我不能这样做,因为我没有15点的声誉((