How can I backup sqlite file in SD Card programmatically?

落花浮王杯 提交于 2019-12-18 03:47:48

问题


When you use emulator your sqlite file is stored in a folder near your main application folder and you can download it. But this feature is not accessible in not rooted devices. How can I backup this existing sqlite file in SD Card programmatically?

I want to have a button in my application that stores this file in a special path in my SD Card. Is it possible?

Thanks,


回答1:


You can try this, work for me, remember to get the WRITE_EXTERNAL_STORAGE permission in your manifest:

// 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:


You can try following code,

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);



回答3:


You can try this to copy a file:

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) {

        }

    }

This is the code to get the filname of your internal db:

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

Replace MY_DB_NAME with the name of your DB



来源:https://stackoverflow.com/questions/12489718/how-can-i-backup-sqlite-file-in-sd-card-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!