#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 // support
implementation "com.android.support:appcompat-v7:$support_version" implementation "com.android.support:appcompat-v7:$support_version"
implementation "com.android.support:exifinterface:$support_version"
// dagger // dagger
implementation "com.google.dagger:dagger:$dagger_version" implementation "com.google.dagger:dagger:$dagger_version"
@@ -5,15 +5,41 @@ import android.content.ContentUris
import android.content.ContentValues import android.content.ContentValues
import android.content.Context import android.content.Context
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Matrix import android.graphics.Matrix
import android.net.Uri import android.net.Uri
import android.provider.MediaStore import android.provider.MediaStore
import android.provider.MediaStore.Images import android.provider.MediaStore.Images
import android.support.media.ExifInterface
import util.tryOrNull import util.tryOrNull
import javax.inject.Inject import javax.inject.Inject
class ImageRepostoryImpl @Inject constructor(private val context: Context) : ImageRepository { 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) { override fun saveImage(uri: Uri) {
val bitmap = tryOrNull { MediaStore.Images.Media.getBitmap(context.contentResolver, uri) } val bitmap = tryOrNull { MediaStore.Images.Media.getBitmap(context.contentResolver, uri) }
@@ -1,9 +1,12 @@
package repository package repository
import android.graphics.Bitmap
import android.net.Uri import android.net.Uri
interface ImageRepository { interface ImageRepository {
fun loadImage(uri: Uri): Bitmap?
fun saveImage(uri: Uri) 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.Message
import com.klinker.android.send_message.Settings import com.klinker.android.send_message.Settings
import com.klinker.android.send_message.Transaction import com.klinker.android.send_message.Transaction
import com.mlsdev.rximagepicker.RxImageConverters
import feature.compose.Attachment import feature.compose.Attachment
import io.reactivex.Flowable import io.reactivex.Flowable
import repository.ImageRepository
import repository.MessageRepository import repository.MessageRepository
import util.Preferences import util.Preferences
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
@@ -34,6 +34,7 @@ import javax.inject.Inject
class SendMessage @Inject constructor( class SendMessage @Inject constructor(
private val context: Context, private val context: Context,
private val imageRepository: ImageRepository,
private val prefs: Preferences, private val prefs: Preferences,
private val messageRepo: MessageRepository private val messageRepo: MessageRepository
) : Interactor<SendMessage.Params>() { ) : Interactor<SendMessage.Params>() {
@@ -72,8 +73,8 @@ class SendMessage @Inject constructor(
var totalImageBytes = 0 var totalImageBytes = 0
attachments attachments
.filter { attachment -> !attachment.isGif(context) } .filter { attachment -> !attachment.isGif(context) }
.map { attachment -> attachment.getUri() } .mapNotNull { attachment -> attachment.getUri() }
.map { uri -> RxImageConverters.uriToBitmap(context, uri).blockingFirst() } .mapNotNull { uri -> imageRepository.loadImage(uri) }
.also { totalImageBytes = it.sumBy { it.allocationByteCount } } .also { totalImageBytes = it.sumBy { it.allocationByteCount } }
.map { bitmap -> .map { bitmap ->
val byteRatio = bitmap.allocationByteCount / totalImageBytes.toFloat() val byteRatio = bitmap.allocationByteCount / totalImageBytes.toFloat()