【问题标题】:How can I backup sqlite file in SD Card programmatically?如何以编程方式备份​​ SD 卡中的 sqlite 文件?
【发布时间】:2012-09-11 11:12:50
【问题描述】:

当您使用模拟器时,您的 sqlite 文件存储在主应用程序文件夹附近的文件夹中,您可以下载它。但是在没有 root 权限的设备中无法访问此功能。如何以编程方式备份​​ SD 卡中现有的 sqlite 文件

我想在我的应用程序中有一个按钮,用于将此文件存储在我的 SD 卡中的特殊路径中。有可能吗?

谢谢,

【问题讨论】:

标签: android database sqlite store sd-card


【解决方案1】:

你可以试试这个,为我工作,记得在你的清单中获得 WRITE_EXTERNAL_STORAGE 权限:

// Copy to sdcard for debug use
    public static void copyDatabase(Context c, String DATABASE_NAME) {
        String databasePath = c.getDatabasePath(DATABASE_NAME).getPath();
        File f = new File(databasePath);
        OutputStream myOutput = null;
        InputStream myInput = null;
        Log.d("testing", " testing db path " + databasePath);
        Log.d("testing", " testing db exist " + f.exists());

        if (f.exists()) {
            try {

                File directory = new File("/mnt/sdcard/DB_DEBUG");
                if (!directory.exists())
                    directory.mkdir();

                myOutput = new FileOutputStream(directory.getAbsolutePath()
                        + "/" + DATABASE_NAME);
                myInput = new FileInputStream(databasePath);

                byte[] buffer = new byte[1024];
                int length;
                while ((length = myInput.read(buffer)) > 0) {
                    myOutput.write(buffer, 0, length);
                }

                myOutput.flush();
            } catch (Exception e) {
            } finally {
                try {
                    if (myOutput != null) {
                        myOutput.close();
                        myOutput = null;
                    }
                    if (myInput != null) {
                        myInput.close();
                        myInput = null;
                    }
                } catch (Exception e) {
                }
            }
        }
    }

【讨论】:

    【解决方案2】:

    你可以试试下面的代码,

    String path = Environment.getExternalStorageDirectory().toString() + "/path";
    File folder = new File( path );
    if (!folder.exists()) 
    {
         folder.mkdirs();
    }
    
    File dbfile = new File( path + "/database.db" ); 
    if ( !dbfile.exists() )
    {
        dbFile.createFile();
    }
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
    

    【讨论】:

    • 这不会复制现有数据库。它在此路径中创建
    • @breceivemail 根据您之前的问题标题如何以编程方式将 sqlite 文件存储在 SD 卡中?,它完全满足要求。
    【解决方案3】:

    你可以试试这个来复制一个文件:

    public void copyFile(File in, File out) {
            String DialogTitel = getString(R.string.daten_wait_titel);
            String DialogText = getString(R.string.kopiervorgang_laeuft);
            try {
                // Dialogdefinition Prograssbar
                final ProgressDialog dialog = new ProgressDialog(this) {
                    @Override
                    public boolean onSearchRequested() {
                        return false;
                    }
                };
                dialog.setCancelable(false);
                dialog.setTitle(DialogTitel);
                dialog.setIcon(R.drawable.icon);
                dialog.setMessage(DialogText);
                dialog.show();
                new Thread(new MyCopyThread(in, out)) {
                    @Override
                    public void run() {
                        try {
                            FileChannel inChannel = new FileInputStream(
                                    MyCopyThread.in).getChannel();
                            FileChannel outChannel = new FileOutputStream(
                                    MyCopyThread.out).getChannel();
                            try {
                                System.out.println("KOPIEREN");
                                inChannel.transferTo(0, inChannel.size(),
                                        outChannel);
                                if (inChannel != null)
                                    inChannel.close();
                                if (outChannel != null)
                                    outChannel.close();
                                setCopyError(false);
                            } catch (IOException e) {
                                setCopyError(true);
                                // throw e;
                            } finally {
                                if (inChannel != null)
                                    inChannel.close();
                                if (outChannel != null)
                                    outChannel.close();
                            }
                            dialog.dismiss();
                            // Abschlussarbeiten
                            if (useExternalSD == true) {
                                // Externe DB
                                moveDBtoExternFinish();
                            } else {
                                // Interne DB
                                moveDBtoInternFinish();
                            }
                            moveDBFinishHandler.sendMessage(moveDBFinishHandler
                                    .obtainMessage());
                        } catch (Exception ex) {
    
                        }
                    }
                }.start();
            } catch (Exception exx) {
    
            }
    
        }
    

    这是获取内部数据库文件名的代码:

    File interneDB = getApplicationContext().getDatabasePath(MY_DB_NAME);
    

    将 MY_DB_NAME 替换为您的数据库名称

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      相关资源
      最近更新 更多