This commit is contained in:
Paul Akhamiogu 2021-09-05 23:17:46 +01:00
parent 664346e8a9
commit 0124c6e2f7
3 changed files with 23 additions and 46 deletions

View File

@ -602,40 +602,23 @@ class ThreadActivity : SimpleActivity() {
attachmentSelections[originalUriString] = AttachmentSelection(uri, false) attachmentSelections[originalUriString] = AttachmentSelection(uri, false)
val attachmentView = addAttachmentView(originalUriString, uri) val attachmentView = addAttachmentView(originalUriString, uri)
val mimeType = contentResolver.getType(uri) val mimeType = contentResolver.getType(uri) ?: return
Log.e(TAG, "Selected image: mimetype=$mimeType uri=$uri")
if (mimeType == null) {
Log.e(TAG, "addAttachment: null mime type for uri: $uri")
return
}
if (mimeType.isImageMimeType()) { 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] val selection = attachmentSelections[originalUriString]
attachmentSelections[originalUriString] = selection!!.copy(isPending = true) attachmentSelections[originalUriString] = selection!!.copy(isPending = true)
checkSendMessageAvailability() checkSendMessageAvailability()
attachmentView.thread_attachment_progress.beVisible() attachmentView.thread_attachment_progress.beVisible()
imageCompressor.compressImage(byteArray, mimeType, IMAGE_COMPRESS_SIZE) { compressedUri -> imageCompressor.compressImage(uri, IMAGE_COMPRESS_SIZE) { compressedUri ->
runOnUiThread { runOnUiThread {
if (compressedUri != null) { if (compressedUri != null) {
Log.e(TAG, "Compressed successfully compressedUri=$compressedUri")
attachmentSelections[originalUriString] = AttachmentSelection(compressedUri, false) attachmentSelections[originalUriString] = AttachmentSelection(compressedUri, false)
loadAttachmentPreview(attachmentView, compressedUri) loadAttachmentPreview(attachmentView, compressedUri)
} else {
Log.e(TAG, "addAttachment: Failed to compress image: uri=$uri")
} }
checkSendMessageAvailability() checkSendMessageAvailability()
attachmentView.thread_attachment_progress.beGone() attachmentView.thread_attachment_progress.beGone()
} }
} }
} else {
Log.d(TAG, "addAttachment: not an image")
} }
} }
@ -661,7 +644,7 @@ class ThreadActivity : SimpleActivity() {
.diskCacheStrategy(DiskCacheStrategy.NONE) .diskCacheStrategy(DiskCacheStrategy.NONE)
.transform(CenterCrop(), RoundedCorners(roundedCornersRadius)) .transform(CenterCrop(), RoundedCorners(roundedCornersRadius))
Glide.with(this) Glide.with(attachmentView.thread_attachment_preview)
.load(uri) .load(uri)
.transition(DrawableTransitionOptions.withCrossFade()) .transition(DrawableTransitionOptions.withCrossFade())
.apply(options) .apply(options)
@ -722,17 +705,13 @@ class ThreadActivity : SimpleActivity() {
if (attachmentSelections.isNotEmpty()) { if (attachmentSelections.isNotEmpty()) {
for (selection in attachmentSelections.values) { for (selection in attachmentSelections.values) {
Log.d(TAG, "sendMessage:attachmentUri=$selection")
try { try {
val byteArray = contentResolver.openInputStream(selection.uri)?.readBytes() ?: continue val byteArray = contentResolver.openInputStream(selection.uri)?.readBytes() ?: continue
val mimeType = contentResolver.getType(selection.uri) ?: continue val mimeType = contentResolver.getType(selection.uri) ?: continue
message.addMedia(byteArray, mimeType) message.addMedia(byteArray, mimeType)
Log.d(TAG, "sendMessage: byteArray: ${byteArray.size} -- mimeType=$mimeType")
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "sendMessage: ", e)
showErrorToast(e) showErrorToast(e)
} catch (e: Error) { } catch (e: Error) {
Log.e(TAG, "sendMessage error: ", e)
toast(e.localizedMessage ?: getString(R.string.unknown_error_occurred)) toast(e.localizedMessage ?: getString(R.string.unknown_error_occurred))
} }
} }

View File

@ -6,12 +6,12 @@ import android.graphics.BitmapFactory
import android.graphics.Matrix import android.graphics.Matrix
import android.media.ExifInterface import android.media.ExifInterface
import android.net.Uri import android.net.Uri
import android.util.Log
import com.simplemobiletools.commons.extensions.getCompressionFormat import com.simplemobiletools.commons.extensions.getCompressionFormat
import com.simplemobiletools.commons.extensions.getMyFileUri import com.simplemobiletools.commons.extensions.getMyFileUri
import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.smsmessenger.extensions.extension import com.simplemobiletools.smsmessenger.extensions.extension
import com.simplemobiletools.smsmessenger.extensions.getExtensionFromMimeType import com.simplemobiletools.smsmessenger.extensions.getExtensionFromMimeType
import com.simplemobiletools.smsmessenger.extensions.isImageMimeType
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
@ -20,33 +20,31 @@ import java.io.FileOutputStream
* [Compressor](https://github.com/zetbaitsu/Compressor/) * [Compressor](https://github.com/zetbaitsu/Compressor/)
* */ * */
class ImageCompressor(private val context: Context) { 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 { private val outputDirectory = File(context.cacheDir, "compressed").apply {
if (!exists()) {
mkdirs() mkdirs()
} }
fun compressImage(byteArray: ByteArray, mimeType: String, 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()}")
} }
Log.d(TAG, "Compression done, new size is ${destinationFile.length()}") fun compressImage(uri: Uri, compressSize: Long, callback: (compressedFileUri: Uri?) -> Unit) {
ensureBackgroundThread {
try {
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)) callback.invoke(context.getMyFileUri(destinationFile))
} else {
callback.invoke(null)
}
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "compressImage: ", e)
callback.invoke(null) callback.invoke(null)
} }
} }

View File

@ -2,8 +2,8 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/conversation_holder" android:id="@+id/conversation_holder"
android:layout_width="wrap_content" android:layout_width="@dimen/attachment_preview_size"
android:layout_height="wrap_content"> android:layout_height="@dimen/attachment_preview_size">
<ImageView <ImageView
android:id="@+id/thread_attachment_preview" android:id="@+id/thread_attachment_preview"