// create directory because it might not exist
@PluginMethod
    public void writeFile(PluginCall call) {
        String path = call.getString("path");
        String data = call.getString("data");
        Boolean recursive = call.getBoolean("recursive", false);
        if (path == null) {
            Logger.error(getLogTag(), "No path or filename retrieved from call", null);
            call.reject("NO_PATH");
            return;
        }
        if (data == null) {
            Logger.error(getLogTag(), "No data retrieved from call", null);
            call.reject("NO_DATA");
            return;
        }
        String directory = getDirectoryParameter(call);
        if (directory != null) {
            if (isPublicDirectory(directory) && !isStoragePermissionGranted()) {
                requestAllPermissions(call, "permissionCallback");
            } else {
                // create directory because it might not exist
                File androidDir = implementation.getDirectory(directory);
                if (androidDir != null) {
                    if (androidDir.exists() || androidDir.mkdirs()) {
                        // path might include directories as well
                        File fileObject = new File(androidDir, path);
                        if (fileObject.getParentFile().exists() || (recursive && fileObject.getParentFile().mkdirs())) {
                            saveFile(call, fileObject, data);
                        } else {
                            call.reject("Parent folder doesn't exist");
                        }
                    } else {
                        Logger.error(getLogTag(), "Not able to create '" + directory + "'!", null);
                        call.reject("NOT_CREATED_DIR");
                    }
                } else {
                    Logger.error(getLogTag(), "Directory ID '" + directory + "' is not supported by plugin", null);
                    call.reject("INVALID_DIR");
                }
            }
        } else {
            // check file:// or no scheme uris
            Uri u = Uri.parse(path);
            if (u.getScheme() == null || u.getScheme().equals("file")) {
                File fileObject = new File(u.getPath());
                // do not know where the file is being store so checking the permission to be secure
                // TODO to prevent permission checking we need a property from the call
                if (!isStoragePermissionGranted()) {
                    requestAllPermissions(call, "permissionCallback");
                } else {
                    if (
                        fileObject.getParentFile() == null ||
                        fileObject.getParentFile().exists() ||
                        (recursive && fileObject.getParentFile().mkdirs())
                    ) {
                        saveFile(call, fileObject, data);
                    } else {
                        call.reject("Parent folder doesn't exist");
                    }
                }
            } else {
                call.reject(u.getScheme() + " scheme not supported");
            }
        }
    }
requestAllPermissions(call, "permissionCallback");
} else {
// create directory because it might not exist
File androidDir = implementation.getDirectory(directory);
if (androidDir != null) {
if (data == null) {
Logger.error(getLogTag(), "No data retrieved from call", null);
call.reject("NO_DATA");
return;
}
String directory = getDirectoryParameter(call);
if (directory != null) {
if (isPublicDirectory(directory) && !isStoragePermissionGranted()) {
requestAllPermissions(call, "permissionCallback");
} else {
// create directory because it might not exist
File androidDir = implementation.getDirectory(directory);
if (androidDir != null) {
if (androidDir.exists() || androidDir.mkdirs()) {
// path might include directories as well
File fileObject = new File(androidDir, path);
if (fileObject.getParentFile().exists() || (recursive && fileObject.getParentFile().mkdirs())) {
saveFile(call, fileObject, data);
} else {
call.reject("Parent folder doesn't exist");
}
@PluginMethod
public void writeFile(PluginCall call) {
String path = call.getString("path");
String data = call.getString("data");
Boolean recursive = call.getBoolean("recursive", false);
if (path == null) {
Logger.error(getLogTag(), "No path or filename retrieved from call", null);
call.reject("NO_PATH");
return;
}
if (data == null) {
Logger.error(getLogTag(), "No data retrieved from call", null);
call.reject("NO_DATA");
return;
}
String directory = getDirectoryParameter(call);
if (directory != null) {
if (isPublicDirectory(directory) && !isStoragePermissionGranted()) {
requestAllPermissions(call, "permissionCallback");
} else {
// create directory because it might not exist
File androidDir = implementation.getDirectory(directory);
if (androidDir != null) {
if (androidDir.exists() || androidDir.mkdirs()) {
// path might include directories as well
File fileObject = new File(androidDir, path);
if (fileObject.getParentFile().exists() || (recursive && fileObject.getParentFile().mkdirs())) {
saveFile(call, fileObject, data);
} else {
call.reject("Parent folder doesn't exist");
}
} else {
Logger.error(getLogTag(), "Not able to create '" + directory + "'!", null);
call.reject("NOT_CREATED_DIR");
}
} else {
Logger.error(getLogTag(), "Directory ID '" + directory + "' is not supported by plugin", null);
call.reject("INVALID_DIR");
}
}
} else {