0

So I am new in programing and i adding a feature in my app which save all sharedpreference key in value in Device internal data folder by using fileoutputStream like this add all data in map and store in jsonobject

private String maptojson(){
    Map<String, ?> map = prf.getAll();
    JSONObject object = new JSONObject();
    for (Map.Entry<String,?> entry : map.entrySet()){
        try {
            object.put( entry.getKey(), entry.getValue());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return object.toString();
}

Now Use FileOutputStream for write file in internal Storage "data'

public void backupSetting() {
    Throwable th;
    FileOutputStream fileOutputStream;
    IOException e;
    FileOutputStream fileOutputStream2 = null;
    File externalStorageDirectory = Environment.getExternalStorageDirectory();
    if (externalStorageDirectory.exists() && externalStorageDirectory.canWrite()) {
        if (externalStorageDirectory.getUsableSpace() >= 1048576) {
            File file = new File(externalStorageDirectory.toString() + "/data/MyApp/" + "MyAppSetting.ma");
            try {
                new File(file.getParent()).mkdirs();
                file.createNewFile();
                fileOutputStream = new FileOutputStream(file);
                try {
                    fileOutputStream.write(maptojson().getBytes());
                    fileOutputStream.flush();
                    fileOutputStream.close();
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException e2) {
                            e2.printStackTrace();
                        }
                    }
                } catch (IOException e3) {
                    e = e3;
                    try {
                        e.printStackTrace();
                        if (fileOutputStream != null) {
                        }
                    } catch (Throwable th2) {
                        th = th2;
                        fileOutputStream2 = fileOutputStream;
                        if (fileOutputStream2 != null) {
                            try {
                                fileOutputStream2.close();
                            } catch (IOException e4) {
                                e4.printStackTrace();
                                throw th;
                            }
                        }
                        throw th;
                    }
                }
            } catch (IOException e5) {
                e = e5;
                fileOutputStream = null;
                e.printStackTrace();
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                    } catch (IOException e6) {
                        e6.printStackTrace();
                    }
                }
            } catch (Throwable th3) {
                th = th3;
                if (fileOutputStream2 != null) {
                }
                try {
                    throw th;
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
            }
        }
    }

Now i want to add restore setting means restore setting by using that file "MyAppSetting.ma". I know i can do it by using FileInputStream but I don't understand how to do? please help if you could

Yuvi15
  • 1
  • 4
  • Are you aware that `externalStorageDirectory` will get you the path to the physical SD-card, not to the shared storage (see https://stackoverflow.com/a/38760040/150978). Additionally be aware that writing to the shared storage as well as to the SD-card requires the app to have appropriate permissions in `AndroidManifest.xml` plus the app have to request those permissions at run-time. In case of the external sd-card you even have to select the path in the file-explorer you grant write access to. Lat but not least direct file access to shared storage only works up to Android 9. – Robert May 17 '21 at 09:17
  • For Android 10+ you need to use scoped storage. Consider tow rite the data to `/data/local/tmp/` instead. All apps should have write access there (but no read access on the directory so you can't list the files). – Robert May 17 '21 at 09:18
  • yah i aware about that but externalStorageDirectory work for me now its write my setting file inside internal "data/MyApp/" and my setting file successfully created in that directory but issue is that i want to read that file as a restore setting backup i am working on a xposed module which change device parameters like imei,device,id those identity parameters for give installed apps to spoof parameters and its working properly but now i want to add a feature for save that spoofed parameters and when user want they can restore it if possible could you contribute my project ? – Yuvi15 May 17 '21 at 10:43
  • You men something like this app? https://github.com/BlueCat300/XposedAppSettings – Robert May 17 '21 at 10:49
  • I fix it by using ObjectOutputStream and ObjectInputStream thanks for help – Yuvi15 Jun 07 '21 at 18:49

0 Answers0