将数据库导出到SD卡

时间:2013-12-23 17:55:40

标签: android sqlite

我需要你的帮助,我完成了一个应用程序的编码,但问题是数据库在系统内:/ data / data / your.package.name,我试图通过Sdcard访问这个数据库,我已经在这个论坛寻找解决方案,有很多,但我不知道如何让它工作,例如:

         private class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> {
    private final ProgressDialog dialog = new ProgressDialog(ctx);

    // can use UI thread here
    protected void onPreExecute() {
       this.dialog.setMessage("Exporting database...");
       this.dialog.show();
    }

    // automatically done on worker thread (separate from UI thread)
    protected Boolean doInBackground(final String... args) {

       File dbFile =
                new File(Environment.getDataDirectory() + "/data/com.mypkg/databases/mydbfile.db");

       File exportDir = new File(Environment.getExternalStorageDirectory(), "");
       if (!exportDir.exists()) {
          exportDir.mkdirs();
       }
       File file = new File(exportDir, dbFile.getName());

       try {
          file.createNewFile();
          this.copyFile(dbFile, file);
          return true;
       } catch (IOException e) {
          Log.e("mypck", e.getMessage(), e);
          return false;
       }
    }

    // can use UI thread here
    protected void onPostExecute(final Boolean success) {
       if (this.dialog.isShowing()) {
          this.dialog.dismiss();
       }
       if (success) {
          Toast.makeText(ctx, "Export successful!", Toast.LENGTH_SHORT).show();
       } else {
          Toast.makeText(ctx, "Export failed", Toast.LENGTH_SHORT).show();
       }
    }

    void copyFile(File src, File dst) throws IOException {
       FileChannel inChannel = new FileInputStream(src).getChannel();
       FileChannel outChannel = new FileOutputStream(dst).getChannel();
       try {
          inChannel.transferTo(0, inChannel.size(), outChannel);
       } finally {
          if (inChannel != null)
             inChannel.close();
          if (outChannel != null)
             outChannel.close();
       }
    }

 }

1 个答案:

答案 0 :(得分:2)

这可能对您有所帮助。

File f=new File("/data/data/YOURAPPPACKAGENAME/databases/YOURDATABASENAME");
                       FileInputStream fis=null;
                     FileOutputStream fos=null;

                    try
                    {
                      fis=new FileInputStream(f);
                      fos=new FileOutputStream("YOURSDCARDFOLDERPATH"+"/YOUREXPORTDDATBASENAME");
                      while(true)
                      {
                        int i=fis.read();
                        if(i!=-1)
                        {fos.write(i);}
                        else
                        {break;}
                      }
                      fos.flush();
                      Toast.makeText(ctx, "DB dump OK", Toast.LENGTH_LONG).show();
                    }
                    catch(Exception e)
                    {
                      e.printStackTrace();
                      Toast.makeText(ctx, "DB dump ERROR", Toast.LENGTH_LONG).show();
                    }
                    finally
                    {
                      try
                      {
                        fos.close();
                        fis.close();
                      }
                      catch(Exception ioe)
                      {}
                    }

您必须在manifest.xml文件中添加sdcard读写权限,并且在数据库成功导出后,您可以使用DDMS工具将此数据库拉入计算机并使用SQLite Manager打开。