Fix `resizeImage()` threading issue

This commit is contained in:
Naveen 2023-05-24 04:20:44 +05:30
parent a1fca17516
commit 591e5f09e9
No known key found for this signature in database
GPG Key ID: 0E155DAD31671DA3
2 changed files with 31 additions and 33 deletions

View File

@ -67,18 +67,18 @@ class ResizeMultipleImagesDialog(
private fun resizeImages(factor: Float) { private fun resizeImages(factor: Float) {
progressView.show() progressView.show()
ensureBackgroundThread { with(activity) {
with(activity) { val newSizes = imageSizes.map {
val newSizes = imageSizes.map { val width = (it.x * factor).roundToInt()
val width = (it.x * factor).roundToInt() val height = (it.y * factor).roundToInt()
val height = (it.y * factor).roundToInt() Point(width, height)
Point(width, height) }
}
val parentPath = imagePaths.first().getParentPath() val parentPath = imagePaths.first().getParentPath()
val pathsToRescan = arrayListOf<String>() val pathsToRescan = arrayListOf<String>()
ensureWriteAccess(parentPath) { ensureWriteAccess(parentPath) {
ensureBackgroundThread {
for (i in imagePaths.indices) { for (i in imagePaths.indices) {
val path = imagePaths[i] val path = imagePaths[i]
val size = newSizes[i] val size = newSizes[i]

View File

@ -823,35 +823,33 @@ fun BaseSimpleActivity.launchResizeImageDialog(path: String, callback: (() -> Un
} }
fun BaseSimpleActivity.resizeImage(path: String, size: Point, callback: (success: Boolean) -> Unit) { fun BaseSimpleActivity.resizeImage(path: String, size: Point, callback: (success: Boolean) -> Unit) {
ensureBackgroundThread { var oldExif: ExifInterface? = null
var oldExif: ExifInterface? = null if (isNougatPlus()) {
if (isNougatPlus()) { val inputStream = contentResolver.openInputStream(Uri.fromFile(File(path)))
val inputStream = contentResolver.openInputStream(Uri.fromFile(File(path))) oldExif = ExifInterface(inputStream!!)
oldExif = ExifInterface(inputStream!!) }
}
val newBitmap = Glide.with(applicationContext).asBitmap().load(path).submit(size.x, size.y).get() val newBitmap = Glide.with(applicationContext).asBitmap().load(path).submit(size.x, size.y).get()
val newFile = File(path) val newFile = File(path)
val newFileDirItem = FileDirItem(path, path.getFilenameFromPath()) val newFileDirItem = FileDirItem(path, path.getFilenameFromPath())
getFileOutputStream(newFileDirItem, true) { out -> getFileOutputStream(newFileDirItem, true) { out ->
if (out != null) { if (out != null) {
out.use { out.use {
try { try {
newBitmap.compress(newFile.absolutePath.getCompressionFormat(), 90, out) newBitmap.compress(newFile.absolutePath.getCompressionFormat(), 90, out)
if (isNougatPlus()) { if (isNougatPlus()) {
val newExif = ExifInterface(newFile.absolutePath) val newExif = ExifInterface(newFile.absolutePath)
oldExif?.copyNonDimensionAttributesTo(newExif) oldExif?.copyNonDimensionAttributesTo(newExif)
}
} catch (ignored: Exception) {
} }
} catch (ignored: Exception) {
callback(true)
} }
} else {
callback(false) callback(true)
} }
} else {
callback(false)
} }
} }
} }