无法创建备份到SD卡

时间:2015-01-15 12:24:05

标签: android sqlite

我试图创建一个数据库备份,但是,它总是返回备份失败。 任何帮助将不胜感激。

这是我的代码:

public class SettingsFragment extends Fragment {

    public SettingsFragment(){}

    Button btnCreateBU, btnRestoreBU;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_settings, container, false);
        btnCreateBU = (Button) rootView.findViewById(R.id.crebutton1);
        btnRestoreBU = (Button) rootView.findViewById(R.id.resbutton2);


        btnCreateBU.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exportDB();
            }
        });

        btnRestoreBU.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                importDB();
            }
        });


        return rootView;
    }


    private void importDB() {
        try {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();
                if (sd.canWrite()) {
                String currentDBPath = "//data//" + "com.my.eclassrecord"
                        + "//databases//" + "data.db";
                String backupDBPath = "/Android/data/com.my.eclassrecord/files/"; // From SD directory.
                File backupDB = new File(data, currentDBPath);
                File currentDB = new File(sd, backupDBPath);

            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
            Toast.makeText(getActivity().getApplicationContext(), "Import Successful!",
                    Toast.LENGTH_SHORT).show();



        }
    } catch (Exception e) {

        Toast.makeText(getActivity().getApplicationContext(), "Import Failed!", Toast.LENGTH_SHORT)
                .show();

    }
}

private void exportDB() {
    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + "com.my.eclassrecord"
                    + "//databases//" + "data.db";
            String backupDBPath = "/Android/data/com.my.eclassrecord/files/";  
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);

            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
            Toast.makeText(getActivity().getApplicationContext(), "Backup Successful!",
                    Toast.LENGTH_SHORT).show();

        }
    } catch (Exception e) {

        Toast.makeText(getActivity().getApplicationContext(), "Backup Failed!", Toast.LENGTH_SHORT)
                .show();

    }
}


}

我还向我的清单声明了以下权限:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />

2 个答案:

答案 0 :(得分:0)

这段代码对我有用!

try {
     File sd = Environment.getExternalStorageDirectory();
     File data = Environment.getDataDirectory();

     if (sd.canWrite()) {
        String currentDBPath = "//data//{package name}//databases//{database name}";
        String backupDBPath = "{database name}";
        File currentDB = new File(data, currentDBPath);
        File backupDB = new File(sd, 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();
        }
    }
 } catch (Exception e) {
 }

您可以发布日志消息吗?它将帮助我们解决问题

答案 1 :(得分:0)

我认为您可以按如下方式解决问题: -

public DatabaseHandler(Context context) {
    super(context, "/storage/sdcard0/"
                    + File.separator + "contacts"
                    + File.separator + DATABASE_NAME, null, DATABASE_VERSION);
}
相关问题