How can I backup SharedPreferences to SD card?

前端 未结 4 942
盖世英雄少女心
盖世英雄少女心 2020-12-08 08:55

I saw in a lot of places that it\'s a problem to copy the SharedPreferences file to the sd card because every manufacturer place it somewhere else.

I want to backup

4条回答
  •  庸人自扰
    2020-12-08 09:08

    File ff = new File("/data/data/"
                                + MainActivity.this.getPackageName()
                                + "/shared_prefs/pref file name.xml");
    
                        Log.i("ffffdffffdffffdffffdd", ff.getPath() + "");
    
                        copyFile(ff.getPath().toString(), sdcard path/save file name.xml");
    
    private void copyFile(String filepath, String storefilepath) {
        try {
            File f1 = new File(filepath);
            File f2 = new File(storefilepath);
            InputStream in = new FileInputStream(f1);
    
            OutputStream out = new FileOutputStream(f2);
    
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
            System.out.println("File copied.");
        } catch (FileNotFoundException ex) {
            System.out.println(ex.getMessage());
    
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    
    }
    

提交回复
热议问题