From c3bfb744f6000c8dacf415f834bac8731a498e70 Mon Sep 17 00:00:00 2001 From: Hai Zhang <4469895+zhanghai@users.noreply.github.com> Date: Sun, 1 May 2022 19:49:05 -0700 Subject: [PATCH] Truncate existing text files when writing. Otherwise if the edit made the text shorter, the remaining content won't be truncated because ContentResolver.openOutputStream(Uri) defaults to "w" only. This wasn't a problem until API 29 because previously the platform ParcelFileDescriptor.parseMode() (incorrectly) treated "w" to be the same as "wt" and added MODE_TRUNCATE. However, that has been fixed since API 29, while the default mode for ContentResolver.openOutputStream(Uri) remained "w", which resulted in such a bug. See also https://stackoverflow.com/q/56902845 . --- .../asynctasks/texteditor/write/WriteTextFileCallable.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/texteditor/write/WriteTextFileCallable.java b/app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/texteditor/write/WriteTextFileCallable.java index f35ad024c..a8ac50f2f 100644 --- a/app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/texteditor/write/WriteTextFileCallable.java +++ b/app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/texteditor/write/WriteTextFileCallable.java @@ -84,13 +84,13 @@ public class WriteTextFileCallable implements Callable { DocumentFile documentFile = DocumentFile.fromSingleUri(AppConfig.getInstance(), fileAbstraction.uri); if (documentFile != null && documentFile.exists() && documentFile.canWrite()) { - outputStream = contentResolver.openOutputStream(fileAbstraction.uri); + outputStream = contentResolver.openOutputStream(fileAbstraction.uri, "wt"); } else { destFile = FileUtils.fromContentUri(fileAbstraction.uri); outputStream = openFile(destFile, context.get()); } } else { - outputStream = contentResolver.openOutputStream(fileAbstraction.uri); + outputStream = contentResolver.openOutputStream(fileAbstraction.uri, "wt"); } break; case FILE: