diff --git a/app/src/main/java/org/pixeldroid/app/postCreation/PostCreationViewModel.kt b/app/src/main/java/org/pixeldroid/app/postCreation/PostCreationViewModel.kt index ef754972..8ec21575 100644 --- a/app/src/main/java/org/pixeldroid/app/postCreation/PostCreationViewModel.kt +++ b/app/src/main/java/org/pixeldroid/app/postCreation/PostCreationViewModel.kt @@ -1,5 +1,6 @@ package org.pixeldroid.app.postCreation +import android.R.attr.orientation import android.app.Application import android.content.ClipData import android.content.Intent @@ -10,10 +11,12 @@ import android.util.Log import android.widget.Toast import androidx.core.net.toFile import androidx.core.net.toUri +import androidx.exifinterface.media.ExifInterface import androidx.lifecycle.* import androidx.preference.PreferenceManager import com.arthenica.ffmpegkit.FFmpegKit import com.jarsilio.android.scrambler.exceptions.UnsupportedFileFormatException +import com.jarsilio.android.scrambler.stripMetadata import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers import io.reactivex.rxjava3.disposables.Disposable import io.reactivex.rxjava3.schedulers.Schedulers @@ -37,9 +40,10 @@ import retrofit2.HttpException import java.io.File import java.io.FileNotFoundException import java.io.IOException +import java.net.URI import javax.inject.Inject import kotlin.math.ceil -import com.jarsilio.android.scrambler.stripMetadata + // Models the UI state for the PostCreationActivity data class PostCreationActivityUiState( @@ -255,22 +259,24 @@ class PostCreationViewModel(application: Application, clipdata: ClipData? = null val strippedImage = File.createTempFile("temp_img", ".$extension", getApplication().cacheDir) + val imageUri = data.imageUri + val (strippedOrNot, size) = try { - stripMetadata(data.imageUri, strippedImage, getApplication().contentResolver) + val orientation = ExifInterface(getApplication().contentResolver.openInputStream(imageUri)!!).getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL) + + stripMetadata(imageUri, strippedImage, getApplication().contentResolver) + + // Restore EXIF orientation + val exifInterface = ExifInterface(strippedImage) + exifInterface.setAttribute(ExifInterface.TAG_ORIENTATION, orientation.toString()) + exifInterface.saveAttributes() + Pair(strippedImage.inputStream(), strippedImage.length()) - } catch (e: FileNotFoundException){ - strippedImage.delete() - _uiState.update { currentUiState -> - currentUiState.copy( - userMessage = getApplication().getString(R.string.file_not_found, - data.imageUri) - ) - } - return - } catch (e: UnsupportedFileFormatException){ + } catch (e: UnsupportedFileFormatException){ strippedImage.delete() + if(imageUri != data.imageUri) File(URI(imageUri.toString())).delete() val imageInputStream = try { - getApplication().contentResolver.openInputStream(data.imageUri)!! + getApplication().contentResolver.openInputStream(imageUri)!! } catch (e: FileNotFoundException){ _uiState.update { currentUiState -> currentUiState.copy( @@ -281,6 +287,16 @@ class PostCreationViewModel(application: Application, clipdata: ClipData? = null return } Pair(imageInputStream, data.size) + } catch (e: IOException){ + strippedImage.delete() + if(imageUri != data.imageUri) File(URI(imageUri.toString())).delete() + _uiState.update { currentUiState -> + currentUiState.copy( + userMessage = getApplication().getString(R.string.file_not_found, + data.imageUri) + ) + } + return } val type = data.imageUri.getMimeType(getApplication().contentResolver) @@ -327,12 +343,14 @@ class PostCreationViewModel(application: Application, clipdata: ClipData? = null ) } strippedImage.delete() + if(imageUri != data.imageUri) File(URI(imageUri.toString())).delete() e.printStackTrace() postSub?.dispose() sub.dispose() }, { strippedImage.delete() + if(imageUri != data.imageUri) File(URI(imageUri.toString())).delete() data.progress = 100 if (getPhotoData().value!!.all { it.progress == 100 && it.uploadId != null }) { _uiState.update { currentUiState -> diff --git a/app/src/main/java/org/pixeldroid/app/utils/Utils.kt b/app/src/main/java/org/pixeldroid/app/utils/Utils.kt index 708c65a1..190361d3 100644 --- a/app/src/main/java/org/pixeldroid/app/utils/Utils.kt +++ b/app/src/main/java/org/pixeldroid/app/utils/Utils.kt @@ -15,7 +15,6 @@ import android.view.WindowManager import android.webkit.MimeTypeMap import androidx.annotation.AttrRes import androidx.annotation.ColorInt -import androidx.annotation.FloatRange import androidx.annotation.StyleRes import androidx.appcompat.app.AppCompatDelegate import androidx.browser.customtabs.CustomTabsIntent