0

我正在使用以下代码将我的数据库副本导出到我的 SD 卡。

public class AgUtility extends AgActivity{

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setContentView(R.layout.utility);
    try {
        backupDatabase(getBaseContext());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}        
public static void backupDatabase(Context context) throws IOException {


    // Open your local db as the input stream
    String inFileName = "data/data/com.agmanagement.todaysstudent/databases/todaysstudent.db";
    Toast.makeText(context, "FileName Is "+ inFileName, Toast.LENGTH_LONG).show();
    Log.i("The File In Is ", inFileName);

    File dbFile = new File(inFileName);
    FileInputStream fis = new FileInputStream(dbFile);

    File outputDirectory = new File(
            Environment.getExternalStorageDirectory() + "/student/");
    outputDirectory.mkdir();
    Log.d("MAKE DIR", dbFile.mkdir() + "");
    String backupFileName = "/TodaysStudentTest.db3"; 
    String outFileName = outputDirectory + backupFileName;
    Toast.makeText(context, "Database backup names is " + outFileName , Toast.LENGTH_LONG)
    .show();  

    // Open the empty db as the output stream
    OutputStream output = new FileOutputStream(outFileName);

    // transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = fis.read(buffer)) > 0) {
        output.write(buffer, 0, length);
    }
    // Close the streams
    output.flush();
    output.close();
    fis.close();

    Toast.makeText(context, "Database backup complete", Toast.LENGTH_LONG)
            .show();
}
}

代码似乎工作正常,因为我没有收到任何错误第一个 Toast 显示正确的数据库名称,第二个 Toast 显示输出目录应该是 mnt/sdcard/student,第三个显示最终目标应该是 mnt/ SD卡/学生/TodaysStudentTest.db3

在 Toast 消失之后,什么都没有,最后的 Toast 永远不会出现。

在我的清单中,我有

我在三星平板电脑上而不是在模拟器上测试它,我也在 DroidX 上运行它,结果相同,没有错误,但没有创建文件夹。

关于我做错了什么的任何想法?TIA

我正在使用的权限是

     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
 <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
 <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
 <uses-permission android:name="android.permission.INTERNET" /> 
 <uses-permission android:name="android.premission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.SET_DEBUG_APP" />
 <uses-permission android:name="android.permission.CAMERA"/>
 <uses-permission android:name="android.permission.READ_CALENDAR"/>
 <uses-permission android:name="android.permission.WRITE_CALENDAR"/>

在模拟器中运行时,我得到相同的结果 - 使用 DDMS 观看 - Logcat show MAKE DIR 失败。

我已经用这个测试了状态

        if (Environment.MEDIA_MOUNTED.equals(state)) {
         // We can read and write the media
         mExternalStorageAvailable = mExternalStorageWriteable = true;
            Toast.makeText(getBaseContext(), "We Can Read And Write To The SDCARD", Toast.LENGTH_LONG).show();
            } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
         // We can only read the media
                mExternalStorageAvailable = true;
                mExternalStorageWriteable = false;
                Toast.makeText(getBaseContext(), "We Can Read The SDCARD", Toast.LENGTH_LONG).show();
            } else {
                // Something else is wrong. It may be one of many other states, but all we need
                //  to know is we can neither read nor write
                mExternalStorageAvailable = mExternalStorageWriteable = false;
                Toast.makeText(getBaseContext(), "We Can't read or write", Toast.LENGTH_LONG).show();
            }

它表明我应该能够读写,所以我的写作方式有问题。我将此添加到文本中

       boolean success = false;
        if(!outputDirectory.exists()){
            Toast.makeText(getBaseContext(), "Folder Doesn't Exist ", Toast.LENGTH_LONG)
            .show();  
            success = outputDirectory.mkdirs();
        }
        if (!success){ 
            Toast.makeText(getBaseContext(), "Folder Not Created ", Toast.LENGTH_LONG)
            .show();  
        }
        else{
            Toast.makeText(getBaseContext(), "Folder Created ", Toast.LENGTH_LONG)
            .show();  
        }

结果是文件夹不存在,然后 mkdirs() 失败。

4

4 回答 4

0

改写

这是处理数据库文件的另一种方法,无需使用 SQL 本身或循环缓冲区。

注意:这实际上并没有复制到 sdcard,备份存储在原始数据库文件夹中(我喜欢它,因为您不需要WRITE_EXTERNAL_STORAGE 权限)。

public class FileIO extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        DBHelper db = new DBHelper(this);

        try { 
            copyFile();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            Log.i("Main", "Complete");
            db.close();
            finish();
        }
    }

    public void copyFile() throws IOException {
        File data = Environment.getDataDirectory();
        String state = Environment.getExternalStorageState();

        /* Create file first
            FileOutputStream created = openFileOutput("copyFile.db", MODE_WORLD_READABLE);
            created.close();
        */

        String currentDBPath = "/data/<your_path>/databases/data.db";
        String backupDBPath = "/data/<your_path>/databases/copyByFile.db";
        File currentDB = new File(data, currentDBPath);
        File backupDB = new File(data, 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.i("Main", "Current db does not exist");
    }
}
于 2012-04-17T16:06:45.457 回答
0

在尝试写入文件之前尝试手动创建文件。

于 2012-04-17T15:17:13.843 回答
0

重要的是要记住检查拼写。uses-permission 被错误拼写为 uses-premission,我已经阅读了很多次代码,我按照我的意愿阅读它。宝贵的教训,走开休息一下。

于 2013-05-31T16:27:14.283 回答
0

请确保您在使用 mkdir() 时已经创建了名为“student”的文件夹。它将通过抽象路径名创建目录..所以如果文件夹“student”不存在,它不会创建新文件夹..或者尝试改为 mkdirs()。如有必要,它将创建父文件夹。

于 2012-04-17T17:47:35.500 回答