reaccepting the EXIF related pull request for some git messup

This commit is contained in:
tibbi 2020-09-22 15:13:59 +02:00
parent 3cb5cf1b91
commit f856a48591
3 changed files with 129 additions and 64 deletions

View file

@ -39,6 +39,7 @@ import com.simplemobiletools.gallery.pro.dialogs.OtherAspectRatioDialog
import com.simplemobiletools.gallery.pro.dialogs.ResizeDialog import com.simplemobiletools.gallery.pro.dialogs.ResizeDialog
import com.simplemobiletools.gallery.pro.dialogs.SaveAsDialog import com.simplemobiletools.gallery.pro.dialogs.SaveAsDialog
import com.simplemobiletools.gallery.pro.extensions.config import com.simplemobiletools.gallery.pro.extensions.config
import com.simplemobiletools.gallery.pro.extensions.copyNonDimensionAttributesTo
import com.simplemobiletools.gallery.pro.extensions.fixDateTaken import com.simplemobiletools.gallery.pro.extensions.fixDateTaken
import com.simplemobiletools.gallery.pro.extensions.openEditor import com.simplemobiletools.gallery.pro.extensions.openEditor
import com.simplemobiletools.gallery.pro.helpers.* import com.simplemobiletools.gallery.pro.helpers.*
@ -196,47 +197,47 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
editor_draw_canvas.beGone() editor_draw_canvas.beGone()
val options = RequestOptions() val options = RequestOptions()
.skipMemoryCache(true) .skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE) .diskCacheStrategy(DiskCacheStrategy.NONE)
Glide.with(this) Glide.with(this)
.asBitmap() .asBitmap()
.load(uri) .load(uri)
.apply(options) .apply(options)
.listener(object : RequestListener<Bitmap> { .listener(object : RequestListener<Bitmap> {
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Bitmap>?, isFirstResource: Boolean): Boolean { override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Bitmap>?, isFirstResource: Boolean): Boolean {
if (uri != originalUri) { if (uri != originalUri) {
uri = originalUri uri = originalUri
Handler().post { Handler().post {
loadDefaultImageView() loadDefaultImageView()
}
} }
return false }
return false
}
override fun onResourceReady(bitmap: Bitmap?, model: Any?, target: Target<Bitmap>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
val currentFilter = getFiltersAdapter()?.getCurrentFilter()
if (filterInitialBitmap == null) {
loadCropImageView()
bottomCropRotateClicked()
} }
override fun onResourceReady(bitmap: Bitmap?, model: Any?, target: Target<Bitmap>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean { if (filterInitialBitmap != null && currentFilter != null && currentFilter.filter.name != getString(R.string.none)) {
val currentFilter = getFiltersAdapter()?.getCurrentFilter() default_image_view.onGlobalLayout {
if (filterInitialBitmap == null) { applyFilter(currentFilter)
loadCropImageView()
bottomCropRotateClicked()
} }
} else {
if (filterInitialBitmap != null && currentFilter != null && currentFilter.filter.name != getString(R.string.none)) { filterInitialBitmap = bitmap
default_image_view.onGlobalLayout {
applyFilter(currentFilter)
}
} else {
filterInitialBitmap = bitmap
}
if (isCropIntent) {
bottom_primary_filter.beGone()
bottom_primary_draw.beGone()
}
return false
} }
}).into(default_image_view)
if (isCropIntent) {
bottom_primary_filter.beGone()
bottom_primary_draw.beGone()
}
return false
}
}).into(default_image_view)
} }
private fun loadCropImageView() { private fun loadCropImageView() {
@ -275,17 +276,17 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
val size = Point() val size = Point()
windowManager.defaultDisplay.getSize(size) windowManager.defaultDisplay.getSize(size)
val options = RequestOptions() val options = RequestOptions()
.format(DecodeFormat.PREFER_ARGB_8888) .format(DecodeFormat.PREFER_ARGB_8888)
.skipMemoryCache(true) .skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE) .diskCacheStrategy(DiskCacheStrategy.NONE)
.fitCenter() .fitCenter()
try { try {
val builder = Glide.with(applicationContext) val builder = Glide.with(applicationContext)
.asBitmap() .asBitmap()
.load(uri) .load(uri)
.apply(options) .apply(options)
.into(editor_draw_canvas.width, editor_draw_canvas.height) .into(editor_draw_canvas.width, editor_draw_canvas.height)
val bitmap = builder.get() val bitmap = builder.get()
runOnUiThread { runOnUiThread {
@ -304,16 +305,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
@TargetApi(Build.VERSION_CODES.N) @TargetApi(Build.VERSION_CODES.N)
private fun saveImage() { private fun saveImage() {
var inputStream: InputStream? = null setOldExif()
try {
if (isNougatPlus()) {
inputStream = contentResolver.openInputStream(uri!!)
oldExif = ExifInterface(inputStream!!)
}
} catch (e: Exception) {
} finally {
inputStream?.close()
}
if (crop_image_view.isVisible()) { if (crop_image_view.isVisible()) {
crop_image_view.getCroppedImageAsync() crop_image_view.getCroppedImageAsync()
@ -354,6 +346,20 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
} }
} }
@TargetApi(Build.VERSION_CODES.N)
private fun setOldExif() {
var inputStream: InputStream? = null
try {
if (isNougatPlus()) {
inputStream = contentResolver.openInputStream(uri!!)
oldExif = ExifInterface(inputStream!!)
}
} catch (e: Exception) {
} finally {
inputStream?.close()
}
}
private fun shareImage() { private fun shareImage() {
ensureBackgroundThread { ensureBackgroundThread {
when { when {
@ -591,17 +597,17 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
val bitmap = try { val bitmap = try {
Glide.with(this) Glide.with(this)
.asBitmap() .asBitmap()
.load(uri).listener(object : RequestListener<Bitmap> { .load(uri).listener(object : RequestListener<Bitmap> {
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Bitmap>?, isFirstResource: Boolean): Boolean { override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Bitmap>?, isFirstResource: Boolean): Boolean {
showErrorToast(e.toString()) showErrorToast(e.toString())
return false return false
} }
override fun onResourceReady(resource: Bitmap?, model: Any?, target: Target<Bitmap>?, dataSource: DataSource?, isFirstResource: Boolean) = false override fun onResourceReady(resource: Bitmap?, model: Any?, target: Target<Bitmap>?, dataSource: DataSource?, isFirstResource: Boolean) = false
}) })
.submit(thumbnailSize, thumbnailSize) .submit(thumbnailSize, thumbnailSize)
.get() .get()
} catch (e: GlideException) { } catch (e: GlideException) {
showErrorToast(e) showErrorToast(e)
finish() finish()
@ -742,6 +748,8 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
override fun onCropImageComplete(view: CropImageView, result: CropImageView.CropResult) { override fun onCropImageComplete(view: CropImageView, result: CropImageView.CropResult) {
if (result.error == null) { if (result.error == null) {
setOldExif()
val bitmap = result.bitmap val bitmap = result.bitmap
if (isSharingBitmap) { if (isSharingBitmap) {
isSharingBitmap = false isSharingBitmap = false
@ -862,7 +870,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
try { try {
if (isNougatPlus()) { if (isNougatPlus()) {
val newExif = ExifInterface(file.absolutePath) val newExif = ExifInterface(file.absolutePath)
oldExif?.copyTo(newExif, false) oldExif?.copyNonDimensionAttributesTo(newExif)
} }
} catch (e: Exception) { } catch (e: Exception) {
} }

View file

@ -986,7 +986,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
if (isNougatPlus()) { if (isNougatPlus()) {
val newExif = ExifInterface(file.absolutePath) val newExif = ExifInterface(file.absolutePath)
oldExif?.copyTo(newExif, false) oldExif?.copyNonDimensionAttributesTo(newExif)
} }
} catch (e: Exception) { } catch (e: Exception) {
} }

View file

@ -0,0 +1,57 @@
package com.simplemobiletools.gallery.pro.extensions
import android.media.ExifInterface
import java.lang.reflect.Field
import java.lang.reflect.Modifier
fun ExifInterface.copyNonDimensionAttributesTo(destination: ExifInterface) {
val attributes = ExifInterfaceAttributes.AllNonDimensionAttributes
attributes.forEach {
val value = getAttribute(it)
if (value != null) {
destination.setAttribute(it, value)
}
}
try {
destination.saveAttributes()
} catch (ignored: Exception) {
}
}
private class ExifInterfaceAttributes {
companion object {
val AllNonDimensionAttributes = getAllNonDimensionExifAttributes()
private fun getAllNonDimensionExifAttributes(): List<String> {
val tagFields = ExifInterface::class.java.fields.filter { field -> isExif(field) }
val excludeAttributes = arrayListOf(
ExifInterface.TAG_IMAGE_LENGTH,
ExifInterface.TAG_IMAGE_WIDTH,
ExifInterface.TAG_PIXEL_X_DIMENSION,
ExifInterface.TAG_PIXEL_Y_DIMENSION,
ExifInterface.TAG_THUMBNAIL_IMAGE_LENGTH,
ExifInterface.TAG_THUMBNAIL_IMAGE_WIDTH,
ExifInterface.TAG_ORIENTATION)
return tagFields
.map { tagField -> tagField.get(null) as String }
.filter { x -> !excludeAttributes.contains(x) }
.distinct()
}
private fun isExif(field: Field): Boolean {
return field.type == String::class.java &&
isPublicStaticFinal(field.modifiers) &&
field.name.startsWith("TAG_")
}
private const val publicStaticFinal = Modifier.PUBLIC or Modifier.STATIC or Modifier.FINAL
private fun isPublicStaticFinal(modifiers: Int): Boolean {
return modifiers and publicStaticFinal > 0
}
}
}