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 69d49502..15942180 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt @@ -602,40 +602,23 @@ class ThreadActivity : SimpleActivity() { attachmentSelections[originalUriString] = AttachmentSelection(uri, false) val attachmentView = addAttachmentView(originalUriString, uri) - val mimeType = contentResolver.getType(uri) - Log.e(TAG, "Selected image: mimetype=$mimeType uri=$uri") - if (mimeType == null) { - Log.e(TAG, "addAttachment: null mime type for uri: $uri") - return - } + val mimeType = contentResolver.getType(uri) ?: return if (mimeType.isImageMimeType()) { - Log.d(TAG, "addAttachment: attachment is an image mimetype=$mimeType") - val byteArray = contentResolver.openInputStream(uri)?.readBytes() - if (byteArray == null) { - Log.e(TAG, "addAttachment: null stream for: $uri") - return - } - val selection = attachmentSelections[originalUriString] attachmentSelections[originalUriString] = selection!!.copy(isPending = true) checkSendMessageAvailability() attachmentView.thread_attachment_progress.beVisible() - imageCompressor.compressImage(byteArray, mimeType, IMAGE_COMPRESS_SIZE) { compressedUri -> + imageCompressor.compressImage(uri, IMAGE_COMPRESS_SIZE) { compressedUri -> runOnUiThread { if (compressedUri != null) { - Log.e(TAG, "Compressed successfully compressedUri=$compressedUri") attachmentSelections[originalUriString] = AttachmentSelection(compressedUri, false) loadAttachmentPreview(attachmentView, compressedUri) - } else { - Log.e(TAG, "addAttachment: Failed to compress image: uri=$uri") } checkSendMessageAvailability() attachmentView.thread_attachment_progress.beGone() } } - } else { - Log.d(TAG, "addAttachment: not an image") } } @@ -661,7 +644,7 @@ class ThreadActivity : SimpleActivity() { .diskCacheStrategy(DiskCacheStrategy.NONE) .transform(CenterCrop(), RoundedCorners(roundedCornersRadius)) - Glide.with(this) + Glide.with(attachmentView.thread_attachment_preview) .load(uri) .transition(DrawableTransitionOptions.withCrossFade()) .apply(options) @@ -722,17 +705,13 @@ class ThreadActivity : SimpleActivity() { if (attachmentSelections.isNotEmpty()) { for (selection in attachmentSelections.values) { - Log.d(TAG, "sendMessage:attachmentUri=$selection") try { val byteArray = contentResolver.openInputStream(selection.uri)?.readBytes() ?: continue val mimeType = contentResolver.getType(selection.uri) ?: continue message.addMedia(byteArray, mimeType) - Log.d(TAG, "sendMessage: byteArray: ${byteArray.size} -- mimeType=$mimeType") } catch (e: Exception) { - Log.e(TAG, "sendMessage: ", e) showErrorToast(e) } catch (e: Error) { - Log.e(TAG, "sendMessage error: ", e) toast(e.localizedMessage ?: getString(R.string.unknown_error_occurred)) } } 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 f642b2a4..50523678 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/ImageCompressor.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/ImageCompressor.kt @@ -6,12 +6,12 @@ import android.graphics.BitmapFactory import android.graphics.Matrix import android.media.ExifInterface import android.net.Uri -import android.util.Log import com.simplemobiletools.commons.extensions.getCompressionFormat import com.simplemobiletools.commons.extensions.getMyFileUri import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.smsmessenger.extensions.extension import com.simplemobiletools.smsmessenger.extensions.getExtensionFromMimeType +import com.simplemobiletools.smsmessenger.extensions.isImageMimeType import java.io.File import java.io.FileOutputStream @@ -20,33 +20,31 @@ import java.io.FileOutputStream * [Compressor](https://github.com/zetbaitsu/Compressor/) * */ class ImageCompressor(private val context: Context) { - companion object { - private const val TAG = "ImageCompressor" - } + private val contentResolver = context.contentResolver private val outputDirectory = File(context.cacheDir, "compressed").apply { - mkdirs() + if (!exists()) { + mkdirs() + } } - fun compressImage(byteArray: ByteArray, mimeType: String, compressSize: Long, callback: (compressedFileUri: Uri?) -> Unit) { + fun compressImage(uri: Uri, compressSize: Long, callback: (compressedFileUri: Uri?) -> Unit) { ensureBackgroundThread { try { - Log.d(TAG, "Attempting to compress image of length: ${byteArray.size} of mimetype=$mimeType to size=$compressSize") - var destinationFile = File(outputDirectory, System.currentTimeMillis().toString().plus(mimeType.getExtensionFromMimeType())) - Log.d(TAG, "compressImage: Saving file to: $destinationFile") - destinationFile.writeBytes(byteArray) - Log.d(TAG, "Written file to: $destinationFile") - val constraint = SizeConstraint(compressSize) - Log.d(TAG, "Starting compression...") - while (constraint.isSatisfied(destinationFile).not()) { - destinationFile = constraint.satisfy(destinationFile) - Log.d(TAG, "Compressed, new size is ${destinationFile.length()}") + val mimeType = contentResolver.getType(uri)!! + if (mimeType.isImageMimeType()) { + 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) + } + callback.invoke(context.getMyFileUri(destinationFile)) + } else { + callback.invoke(null) } - - Log.d(TAG, "Compression done, new size is ${destinationFile.length()}") - callback.invoke(context.getMyFileUri(destinationFile)) } catch (e: Exception) { - Log.e(TAG, "compressImage: ", e) callback.invoke(null) } } diff --git a/app/src/main/res/layout/item_attachment.xml b/app/src/main/res/layout/item_attachment.xml index 8a620499..be794651 100644 --- a/app/src/main/res/layout/item_attachment.xml +++ b/app/src/main/res/layout/item_attachment.xml @@ -2,8 +2,8 @@ + android:layout_width="@dimen/attachment_preview_size" + android:layout_height="@dimen/attachment_preview_size">