#915 - Fix rotation when sending images

This commit is contained in:
Moez Bhatti
2018-05-14 18:09:48 -04:00
parent c4d41c1a12
commit 8374386734
4 changed files with 34 additions and 3 deletions
+1
View File
@@ -15,6 +15,7 @@ dependencies {
// support
implementation "com.android.support:appcompat-v7:$support_version"
implementation "com.android.support:exifinterface:$support_version"
// dagger
implementation "com.google.dagger:dagger:$dagger_version"
@@ -5,15 +5,41 @@ import android.content.ContentUris
import android.content.ContentValues
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Matrix
import android.net.Uri
import android.provider.MediaStore
import android.provider.MediaStore.Images
import android.support.media.ExifInterface
import util.tryOrNull
import javax.inject.Inject
class ImageRepostoryImpl @Inject constructor(private val context: Context) : ImageRepository {
override fun loadImage(uri: Uri): Bitmap? {
val exif = ExifInterface(context.contentResolver.openInputStream(uri))
val bitmap = BitmapFactory.decodeStream(context.contentResolver.openInputStream(uri))
val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
return when (orientation) {
ExifInterface.ORIENTATION_ROTATE_90 -> rotateBitmap(bitmap, 90f)
ExifInterface.ORIENTATION_ROTATE_180 -> rotateBitmap(bitmap, 180f)
ExifInterface.ORIENTATION_ROTATE_270 -> rotateBitmap(bitmap, 270f)
else -> bitmap
}
}
private fun rotateBitmap(bitmap: Bitmap, degree: Float): Bitmap {
val w = bitmap.width
val h = bitmap.height
val mtx = Matrix()
mtx.postRotate(degree)
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true)
}
override fun saveImage(uri: Uri) {
val bitmap = tryOrNull { MediaStore.Images.Media.getBitmap(context.contentResolver, uri) }
@@ -1,9 +1,12 @@
package repository
import android.graphics.Bitmap
import android.net.Uri
interface ImageRepository {
fun loadImage(uri: Uri): Bitmap?
fun saveImage(uri: Uri)
}
@@ -24,9 +24,9 @@ import com.google.android.mms.ContentType
import com.klinker.android.send_message.Message
import com.klinker.android.send_message.Settings
import com.klinker.android.send_message.Transaction
import com.mlsdev.rximagepicker.RxImageConverters
import feature.compose.Attachment
import io.reactivex.Flowable
import repository.ImageRepository
import repository.MessageRepository
import util.Preferences
import java.io.ByteArrayOutputStream
@@ -34,6 +34,7 @@ import javax.inject.Inject
class SendMessage @Inject constructor(
private val context: Context,
private val imageRepository: ImageRepository,
private val prefs: Preferences,
private val messageRepo: MessageRepository
) : Interactor<SendMessage.Params>() {
@@ -72,8 +73,8 @@ class SendMessage @Inject constructor(
var totalImageBytes = 0
attachments
.filter { attachment -> !attachment.isGif(context) }
.map { attachment -> attachment.getUri() }
.map { uri -> RxImageConverters.uriToBitmap(context, uri).blockingFirst() }
.mapNotNull { attachment -> attachment.getUri() }
.mapNotNull { uri -> imageRepository.loadImage(uri) }
.also { totalImageBytes = it.sumBy { it.allocationByteCount } }
.map { bitmap ->
val byteRatio = bitmap.allocationByteCount / totalImageBytes.toFloat()