@@ -128,7 +128,7 @@ class ReadTextActivity : SimpleActivity() {
128128 lastSavePromptTS = System .currentTimeMillis()
129129 ConfirmationAdvancedDialog (this , " " , R .string.save_before_closing, R .string.save, R .string.discard) {
130130 if (it) {
131- saveText (true )
131+ saveAsText (true )
132132 } else {
133133 super .onBackPressed()
134134 }
@@ -144,6 +144,7 @@ class ReadTextActivity : SimpleActivity() {
144144 when (menuItem.itemId) {
145145 R .id.menu_search -> openSearch()
146146 R .id.menu_save -> saveText()
147+ R .id.menu_save_as -> saveAsText()
147148 R .id.menu_open_with -> openPath(intent.dataString!! , true )
148149 R .id.menu_print -> printText()
149150 else -> return @setOnMenuItemClickListener false
@@ -165,10 +166,14 @@ class ReadTextActivity : SimpleActivity() {
165166 }, 250 )
166167 }
167168
168- private fun saveText ( shouldExitAfterSaving : Boolean = false) {
169+ private fun getFilePath () {
169170 if (filePath.isEmpty()) {
170171 filePath = getRealPathFromURI(intent.data!! ) ? : " "
171172 }
173+ }
174+
175+ private fun saveAsText (shouldExitAfterSaving : Boolean = false) {
176+ getFilePath()
172177
173178 if (filePath.isEmpty()) {
174179 SaveAsDialog (this , filePath, true ) { _, filename ->
@@ -182,6 +187,7 @@ class ReadTextActivity : SimpleActivity() {
182187 } else {
183188 SELECT_SAVE_FILE_INTENT
184189 }
190+ @Suppress(" DEPRECATION" )
185191 startActivityForResult(this , requestCode)
186192 }
187193 }
@@ -200,6 +206,20 @@ class ReadTextActivity : SimpleActivity() {
200206 }
201207 }
202208
209+ private fun saveText (shouldExitAfterSaving : Boolean = false) {
210+ getFilePath()
211+
212+ if (hasStoragePermission()) {
213+ val file = File (filePath)
214+ getFileOutputStream(file.toFileDirItem(this ), true ) {
215+ val shouldOverwriteOriginalText = true
216+ saveTextContent(it, shouldExitAfterSaving, shouldOverwriteOriginalText)
217+ }
218+ } else {
219+ toast(R .string.no_storage_permissions)
220+ }
221+ }
222+
203223 private fun saveTextContent (outputStream : OutputStream ? , shouldExitAfterSaving : Boolean , shouldOverwriteOriginalText : Boolean ) {
204224 if (outputStream != null ) {
205225 val currentText = binding.readTextView.text.toString()
0 commit comments