Android中SD卡的备份/恢复

Android中SD卡的备份/恢复,android,database,sd-card,Android,Database,Sd Card,我正在尝试从“首选项”活动将我的应用程序数据库备份/还原到外部SD卡。 我能够将数据库保存到外部SD卡,但现在我不明白如何将此文件传输回他的默认路径(data\package.app\databases)。有什么想法吗?我是这样做的: 出口: InputStream myInput; String dbpath = "/data/"+pckgname+"/databases/refuel_db"; String sdpath = Environme

我正在尝试从“首选项”活动将我的应用程序数据库备份/还原到外部SD卡。 我能够将数据库保存到外部SD卡,但现在我不明白如何将此文件传输回他的默认路径(data\package.app\databases)。有什么想法吗?

我是这样做的:

出口:

InputStream myInput;
            String dbpath = "/data/"+pckgname+"/databases/refuel_db";
            String sdpath = Environment.getExternalStorageDirectory().getPath();

            try {

                myInput = new FileInputStream(Environment.getDataDirectory()
                        + dbpath);


                // Set the output folder on the Scard
                File directory = new File(sdpath + "/Refuel");
                // Create the folder if it doesn't exist:
                if (!directory.exists()) {
                    directory.mkdirs();
                }
                // Set the output file stream up:

                OutputStream myOutput = new FileOutputStream(directory.getPath()
                        + "/refuel_db");

                // Transfer bytes from the input file to the output file
                byte[] buffer = new byte[1024];
                int length;
                while ((length = myInput.read(buffer)) > 0) {
                    myOutput.write(buffer, 0, length);
                }
                // Close and clear the streams

                myOutput.flush();

                myOutput.close();

                myInput.close();

                Toast.makeText(getActivity(), "Backup Done Succesfully!", Toast.LENGTH_LONG)
                        .show();

            } catch (FileNotFoundException e) {
                Toast.makeText(getActivity(), "ERROR " + e, Toast.LENGTH_LONG).show();

                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                Toast.makeText(getActivity(), "ERROR " + e, Toast.LENGTH_LONG).show();

                // TODO Auto-generated catch block
                e.printStackTrace();
            }
进口:

 OutputStream myOutput;

            String dbpath = "/data/"+pckgname+"/databases/refuel_db";
            String sdpath = Environment.getExternalStorageDirectory().getPath();

            try {

                myOutput = new FileOutputStream(Environment.getDataDirectory()
                        + dbpath);

                // Set the folder on the SDcard
                File directory = new File(sdpath + "/Refuel");
                // Set the input file stream up:

                InputStream myInputs = new FileInputStream(directory.getPath()
                        + "/refuel_db");

                // Transfer bytes from the input file to the output file
                byte[] buffer = new byte[1024];
                int length;
                while ((length = myInputs.read(buffer)) > 0) {
                    myOutput.write(buffer, 0, length);
                }

                // Close and clear the streams
                myOutput.flush();

                myOutput.close();

                myInputs.close();

                Toast.makeText(getActivity(), "Import Done Succesfully!", Toast.LENGTH_LONG)
                        .show();

            } catch (FileNotFoundException e) {
                Toast.makeText(getActivity(), "ERROR " + e, Toast.LENGTH_LONG).show();

                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                Toast.makeText(getActivity(), "ERROR " + e, Toast.LENGTH_LONG).show();

                // TODO Auto-generated catch block
                e.printStackTrace();
            }
我是这样做的:

出口:

InputStream myInput;
            String dbpath = "/data/"+pckgname+"/databases/refuel_db";
            String sdpath = Environment.getExternalStorageDirectory().getPath();

            try {

                myInput = new FileInputStream(Environment.getDataDirectory()
                        + dbpath);


                // Set the output folder on the Scard
                File directory = new File(sdpath + "/Refuel");
                // Create the folder if it doesn't exist:
                if (!directory.exists()) {
                    directory.mkdirs();
                }
                // Set the output file stream up:

                OutputStream myOutput = new FileOutputStream(directory.getPath()
                        + "/refuel_db");

                // Transfer bytes from the input file to the output file
                byte[] buffer = new byte[1024];
                int length;
                while ((length = myInput.read(buffer)) > 0) {
                    myOutput.write(buffer, 0, length);
                }
                // Close and clear the streams

                myOutput.flush();

                myOutput.close();

                myInput.close();

                Toast.makeText(getActivity(), "Backup Done Succesfully!", Toast.LENGTH_LONG)
                        .show();

            } catch (FileNotFoundException e) {
                Toast.makeText(getActivity(), "ERROR " + e, Toast.LENGTH_LONG).show();

                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                Toast.makeText(getActivity(), "ERROR " + e, Toast.LENGTH_LONG).show();

                // TODO Auto-generated catch block
                e.printStackTrace();
            }
进口:

 OutputStream myOutput;

            String dbpath = "/data/"+pckgname+"/databases/refuel_db";
            String sdpath = Environment.getExternalStorageDirectory().getPath();

            try {

                myOutput = new FileOutputStream(Environment.getDataDirectory()
                        + dbpath);

                // Set the folder on the SDcard
                File directory = new File(sdpath + "/Refuel");
                // Set the input file stream up:

                InputStream myInputs = new FileInputStream(directory.getPath()
                        + "/refuel_db");

                // Transfer bytes from the input file to the output file
                byte[] buffer = new byte[1024];
                int length;
                while ((length = myInputs.read(buffer)) > 0) {
                    myOutput.write(buffer, 0, length);
                }

                // Close and clear the streams
                myOutput.flush();

                myOutput.close();

                myInputs.close();

                Toast.makeText(getActivity(), "Import Done Succesfully!", Toast.LENGTH_LONG)
                        .show();

            } catch (FileNotFoundException e) {
                Toast.makeText(getActivity(), "ERROR " + e, Toast.LENGTH_LONG).show();

                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                Toast.makeText(getActivity(), "ERROR " + e, Toast.LENGTH_LONG).show();

                // TODO Auto-generated catch block
                e.printStackTrace();
            }

这就是它的工作原理:应用程序A有一个数据库,应用程序B有备份应用程序A数据库的代码吗?@angel您需要从同一个文件夹导出/导入!这样就可以了:)现在您必须像以下字符串一样编写它:dbpath=“//data//”+getpckgname()+“//databases//dbname.db”;否则一切都会正常工作,这就是它的工作原理:应用程序A有一个数据库,应用程序B有这个代码用于备份应用程序A的数据库?@angel您需要从同一个文件夹导出/导入!这样就可以了:)现在您必须像以下字符串一样编写它:dbpath=“//data//”+getpckgname()+“//databases//dbname.db”;否则一切正常