diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt index d3d25027..64f9e0d0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt @@ -677,6 +677,9 @@ class ThreadActivity : SimpleActivity() { if (compressedUri != null) { attachmentSelections[originalUriString] = AttachmentSelection(compressedUri, false) loadAttachmentPreview(attachmentView, compressedUri) + } else { + toast(R.string.compress_error) + removeAttachment(attachmentView, originalUriString) } checkSendMessageAvailability() attachmentView.thread_attachment_progress.beGone() @@ -690,11 +693,7 @@ class ThreadActivity : SimpleActivity() { val attachmentView = layoutInflater.inflate(R.layout.item_attachment, null).apply { thread_attachments_wrapper.addView(this) thread_remove_attachment.setOnClickListener { - thread_attachments_wrapper.removeView(this) - attachmentSelections.remove(originalUri) - if (attachmentSelections.isEmpty()) { - thread_attachments_holder.beGone() - } + removeAttachment(this, originalUri) } } @@ -733,6 +732,14 @@ class ThreadActivity : SimpleActivity() { .into(attachmentView.thread_attachment_preview) } + private fun removeAttachment(attachmentView: View, originalUri: String) { + thread_attachments_wrapper.removeView(attachmentView) + attachmentSelections.remove(originalUri) + if (attachmentSelections.isEmpty()) { + thread_attachments_holder.beGone() + } + } + private fun checkSendMessageAvailability() { if (thread_type_message.text.isNotEmpty() || (attachmentSelections.isNotEmpty() && !attachmentSelections.values.any { it.isPending })) { thread_send_message.isClickable = true diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/ImageCompressor.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/ImageCompressor.kt index aa58387e..363c2960 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/ImageCompressor.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/ImageCompressor.kt @@ -38,9 +38,19 @@ class ImageCompressor(private val context: Context) { val byteArray = contentResolver.openInputStream(uri)?.readBytes()!! var destinationFile = File(outputDirectory, System.currentTimeMillis().toString().plus(mimeType.getExtensionFromMimeType())) destinationFile.writeBytes(byteArray) - val constraint = SizeConstraint(compressSize) - while (constraint.isSatisfied(destinationFile).not()) { - destinationFile = constraint.satisfy(destinationFile) + val sizeConstraint = SizeConstraint(compressSize) + val bitmap = loadBitmap(destinationFile) + + // if image weight > * 2 targeted size: cut down resolution by 2 + if (fileSize > 2 * compressSize) { + val resConstraint = ResolutionConstraint(bitmap.width / 2, bitmap.height / 2) + while (resConstraint.isSatisfied(destinationFile).not()) { + destinationFile = resConstraint.satisfy(destinationFile) + } + } + // do compression + while (sizeConstraint.isSatisfied(destinationFile).not()) { + destinationFile = sizeConstraint.satisfy(destinationFile) } callback.invoke(context.getMyFileUri(destinationFile)) } else { @@ -106,7 +116,11 @@ class ImageCompressor(private val context: Context) { private var iteration: Int = 0 fun isSatisfied(imageFile: File): Boolean { - return imageFile.length() <= maxFileSize || iteration >= maxIteration + // If size requirement is not met and maxIteration is reached + if(iteration >= maxIteration && imageFile.length() >= maxFileSize) { + throw Exception("Unable to compress image to targeted size") + } + return imageFile.length() <= maxFileSize } fun satisfy(imageFile: File): File { @@ -116,4 +130,55 @@ class ImageCompressor(private val context: Context) { } } + private inner class ResolutionConstraint(private val width: Int, private val height: Int) { + + private fun decodeSampledBitmapFromFile(imageFile: File, reqWidth: Int, reqHeight: Int): Bitmap { + return BitmapFactory.Options().run { + inJustDecodeBounds = true + BitmapFactory.decodeFile(imageFile.absolutePath, this) + + inSampleSize = calculateInSampleSize(this, reqWidth, reqHeight) + + inJustDecodeBounds = false + BitmapFactory.decodeFile(imageFile.absolutePath, this) + } + } + + private fun calculateInSampleSize(options: BitmapFactory.Options, reqWidth: Int, reqHeight: Int): Int { + // Raw height and width of image + val (height: Int, width: Int) = options.run { outHeight to outWidth } + var inSampleSize = 1 + + if (height > reqHeight || width > reqWidth) { + + val halfHeight: Int = height / 2 + val halfWidth: Int = width / 2 + + // Calculate the largest inSampleSize value that is a power of 2 and keeps both + // height and width larger than the requested height and width. + while (halfHeight / inSampleSize >= reqHeight && halfWidth / inSampleSize >= reqWidth) { + inSampleSize *= 2 + } + } + + return inSampleSize + } + + fun isSatisfied(imageFile: File): Boolean { + return BitmapFactory.Options().run { + inJustDecodeBounds = true + BitmapFactory.decodeFile(imageFile.absolutePath, this) + calculateInSampleSize(this, width, height) <= 1 + } + } + + fun satisfy(imageFile: File): File { + return decodeSampledBitmapFromFile(imageFile, width, height).run { + determineImageRotation(imageFile, this).run { + overWrite(imageFile, this) + } + } + } + } + } diff --git a/app/src/main/res/values-ar/strings.xml b/app/src/main/res/values-ar/strings.xml index ce8cb029..81b518b3 100644 --- a/app/src/main/res/values-ar/strings.xml +++ b/app/src/main/res/values-ar/strings.xml @@ -19,6 +19,7 @@ تثبيت في الأعلى ازالة التثبيت اعادة ارسال + Unable to compress image to selected size محادثة جديدة إضافة جهة اتصال أو رقم … diff --git a/app/src/main/res/values-ca/strings.xml b/app/src/main/res/values-ca/strings.xml index 780e0ada..3931ce6b 100644 --- a/app/src/main/res/values-ca/strings.xml +++ b/app/src/main/res/values-ca/strings.xml @@ -19,6 +19,7 @@ Fixa a la part superior No fixis Reenvia + Unable to compress image to selected size Conversa nova Afegeix un contacte o número… @@ -74,4 +75,4 @@ --> La mida dels MMS està limitada pels operadors, podeu provar d\'establir un límit més petit a la configuració de l\'aplicació. L\'altre extrem no ha rebut el meu MMS, hi puc fer alguna cosa\? - \ No newline at end of file + diff --git a/app/src/main/res/values-cs/strings.xml b/app/src/main/res/values-cs/strings.xml index 03abd718..43b75451 100644 --- a/app/src/main/res/values-cs/strings.xml +++ b/app/src/main/res/values-cs/strings.xml @@ -19,6 +19,7 @@ Pin to the top Unpin Forward + Unable to compress image to selected size Nová konverzace Přidejte kontakt nebo číslo… diff --git a/app/src/main/res/values-da/strings.xml b/app/src/main/res/values-da/strings.xml index 72b511fe..d2d68c52 100644 --- a/app/src/main/res/values-da/strings.xml +++ b/app/src/main/res/values-da/strings.xml @@ -18,6 +18,7 @@ Pin to the top Unpin Forward + Unable to compress image to selected size Ny Samtale diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 2ba588b5..bb2c1c5c 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -19,6 +19,7 @@ Oben anheften Losheften Weiterleiten + Unable to compress image to selected size Neuer Chat Kontakt oder Nummer hinzufügen … diff --git a/app/src/main/res/values-el/strings.xml b/app/src/main/res/values-el/strings.xml index 7f966df4..04eb95b8 100644 --- a/app/src/main/res/values-el/strings.xml +++ b/app/src/main/res/values-el/strings.xml @@ -18,6 +18,7 @@ Καρφίτσωμα στην κορυφή Ξεκαρφίτσωμα Προώθηση + Unable to compress image to selected size Νέα συνομιλία diff --git a/app/src/main/res/values-eo/strings.xml b/app/src/main/res/values-eo/strings.xml index 526b91ed..35cb4a85 100644 --- a/app/src/main/res/values-eo/strings.xml +++ b/app/src/main/res/values-eo/strings.xml @@ -19,6 +19,7 @@ Alpingli supren Depingli Forward + Unable to compress image to selected size New conversation Add Contact or Number… diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 07dd1316..890cf436 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -18,6 +18,7 @@ Pin to the top Unpin Forward + Unable to compress image to selected size Nueva conversación diff --git a/app/src/main/res/values-et/strings.xml b/app/src/main/res/values-et/strings.xml index f9d7ada1..49223529 100644 --- a/app/src/main/res/values-et/strings.xml +++ b/app/src/main/res/values-et/strings.xml @@ -19,6 +19,7 @@ Pin to the top Eemalda kinnitus Edasta + Unable to compress image to selected size Uus vestlus Add Contact or Number… @@ -74,4 +75,4 @@ Haven't found some strings? There's more at https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res --> - \ No newline at end of file + diff --git a/app/src/main/res/values-fi/strings.xml b/app/src/main/res/values-fi/strings.xml index a382cc2e..e670af6e 100644 --- a/app/src/main/res/values-fi/strings.xml +++ b/app/src/main/res/values-fi/strings.xml @@ -19,6 +19,7 @@ Pin to the top Unpin Forward + Unable to compress image to selected size Uusi keskustelu Lisää yhteystieto tai numero… diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 136013aa..28d335e5 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -19,6 +19,7 @@ Épingler en haut Désépingler Transférer + Unable to compress image to selected size Nouvelle conversation Ajouter un contact ou un numéro… @@ -75,4 +76,4 @@ Haven't found some strings? There's more at https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res --> - \ No newline at end of file + diff --git a/app/src/main/res/values-gl/strings.xml b/app/src/main/res/values-gl/strings.xml index 5546ffd9..70089441 100644 --- a/app/src/main/res/values-gl/strings.xml +++ b/app/src/main/res/values-gl/strings.xml @@ -18,6 +18,7 @@ Pin to the top Unpin Forward + Unable to compress image to selected size Nova conversa diff --git a/app/src/main/res/values-hu/strings.xml b/app/src/main/res/values-hu/strings.xml index 5b9e1bb1..897ef94f 100644 --- a/app/src/main/res/values-hu/strings.xml +++ b/app/src/main/res/values-hu/strings.xml @@ -19,6 +19,7 @@ Kitűzés fentre Kitűzés megszüntetése Továbbítás + Unable to compress image to selected size Új beszélgetés Névjegy vagy szám hozzáadása… @@ -74,4 +75,4 @@ --> A másik fél nem kapja meg az MMS-emet, tehetek valamit ez ellen\? Az MMS méretét korlátozzák a szolgáltatók, próbáljon kisebb korlátot beállítani az alkalmazásbeállításokban. - \ No newline at end of file + diff --git a/app/src/main/res/values-id/strings.xml b/app/src/main/res/values-id/strings.xml index 93dda0ef..e9318417 100644 --- a/app/src/main/res/values-id/strings.xml +++ b/app/src/main/res/values-id/strings.xml @@ -18,6 +18,7 @@ Pin to the top Unpin Forward + Unable to compress image to selected size Percakapan baru diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index 66689ec1..e617434d 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -19,6 +19,7 @@ Fissa in alto Rimuovi Inoltra + Unable to compress image to selected size Nuova conversazione Inserisci contatto o numero… diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 23de867b..554133e9 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -18,6 +18,7 @@ Pin to the top Unpin Forward + Unable to compress image to selected size 新しい会話 diff --git a/app/src/main/res/values-lt/strings.xml b/app/src/main/res/values-lt/strings.xml index 0eb4df9c..7b3b078b 100644 --- a/app/src/main/res/values-lt/strings.xml +++ b/app/src/main/res/values-lt/strings.xml @@ -19,6 +19,7 @@ Pin to the top Unpin Forward + Unable to compress image to selected size Naujas pokalbis Pridėti kontaktą arba numerį… @@ -93,4 +94,4 @@ Haven't found some strings? There's more at https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res --> - \ No newline at end of file + diff --git a/app/src/main/res/values-ml/strings.xml b/app/src/main/res/values-ml/strings.xml index 184f33a0..2c8ff0b5 100644 --- a/app/src/main/res/values-ml/strings.xml +++ b/app/src/main/res/values-ml/strings.xml @@ -18,6 +18,7 @@ Pin to the top Unpin Forward + Unable to compress image to selected size പുതിയ സംഭാഷണം diff --git a/app/src/main/res/values-nb-rNO/strings.xml b/app/src/main/res/values-nb-rNO/strings.xml index fc717306..dbe710ff 100644 --- a/app/src/main/res/values-nb-rNO/strings.xml +++ b/app/src/main/res/values-nb-rNO/strings.xml @@ -19,6 +19,7 @@ Fest til toppen Løsne Videresend + Unable to compress image to selected size Ny samtale Legg til kontakt eller nummer … diff --git a/app/src/main/res/values-nl/strings.xml b/app/src/main/res/values-nl/strings.xml index 162fb7f4..7ae78a14 100644 --- a/app/src/main/res/values-nl/strings.xml +++ b/app/src/main/res/values-nl/strings.xml @@ -18,6 +18,7 @@ Bovenaan vastzetten Losmaken Doorsturen + Unable to compress image to selected size Nieuw gesprek diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index 7547dc6c..60518174 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -19,6 +19,7 @@ Przypnij na górze Odepnij Przekaż dalej + Unable to compress image to selected size Nowa rozmowa Dodaj kontakt lub numer… @@ -96,4 +97,4 @@ Haven't found some strings? There's more at https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res --> - \ No newline at end of file + diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xml index 1e4bf653..cc787e21 100644 --- a/app/src/main/res/values-pt/strings.xml +++ b/app/src/main/res/values-pt/strings.xml @@ -19,6 +19,7 @@ Fixar no topo Desafixar Reencaminhar + Unable to compress image to selected size Nova conversa Adicionar contacto ou número… diff --git a/app/src/main/res/values-ro/strings.xml b/app/src/main/res/values-ro/strings.xml index 5d1abb3a..c791101b 100644 --- a/app/src/main/res/values-ro/strings.xml +++ b/app/src/main/res/values-ro/strings.xml @@ -18,6 +18,7 @@ Fixare în vârf Elimină fixarea Redirecţionare + Unable to compress image to selected size Conversaţie nouă diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 0701e2b4..e725f859 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -19,6 +19,7 @@ Закрепить сверху Открепить Переслать + Unable to compress image to selected size Новая переписка Добавить контакт или номер… @@ -96,4 +97,4 @@ Haven't found some strings? There's more at https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res --> - \ No newline at end of file + diff --git a/app/src/main/res/values-sk/strings.xml b/app/src/main/res/values-sk/strings.xml index 948ae19f..9fecf4e4 100644 --- a/app/src/main/res/values-sk/strings.xml +++ b/app/src/main/res/values-sk/strings.xml @@ -18,6 +18,7 @@ Pripnúť na vrch Odopnúť Preposlať + Unable to compress image to selected size Nová konverzácia diff --git a/app/src/main/res/values-ta/strings.xml b/app/src/main/res/values-ta/strings.xml index 193fbed0..e3a5ee93 100644 --- a/app/src/main/res/values-ta/strings.xml +++ b/app/src/main/res/values-ta/strings.xml @@ -19,6 +19,7 @@ மேலே பொருத்தவும் அன்பின் முன்னோக்கி + Unable to compress image to selected size புதிய உரையாடல் தொடர்பு அல்லது எண்ணைச் சேர்க்கவும்… diff --git a/app/src/main/res/values-tr/strings.xml b/app/src/main/res/values-tr/strings.xml index 593685f1..9047af54 100644 --- a/app/src/main/res/values-tr/strings.xml +++ b/app/src/main/res/values-tr/strings.xml @@ -19,6 +19,7 @@ En üste sabitle Sabitlemeyi kaldır İlet + Unable to compress image to selected size Yeni görüşme Kişi veya Numara Ekle… @@ -94,4 +95,4 @@ Haven't found some strings? There's more at https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res --> - \ No newline at end of file + diff --git a/app/src/main/res/values-uk/strings.xml b/app/src/main/res/values-uk/strings.xml index bbe46f05..ffd625ab 100644 --- a/app/src/main/res/values-uk/strings.xml +++ b/app/src/main/res/values-uk/strings.xml @@ -18,6 +18,7 @@ Pin to the top Unpin Forward + Unable to compress image to selected size Нове листування diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 2f8ed109..60c2b1d1 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -19,6 +19,7 @@ 置顶固定 取消固定 转发 + Unable to compress image to selected size 新的对话 添加联系人或者号码… @@ -94,4 +95,4 @@ Haven't found some strings? There's more at https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res --> - \ No newline at end of file + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 74042531..658870b1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -19,6 +19,7 @@ Pin to the top Unpin Forward + Unable to compress image to selected size New conversation Add Contact or Number…