fix rare crash when resizing image in EditProfileActivity & prevent upscaling of image

This commit is contained in:
Conny Duck 2018-02-15 19:24:01 +01:00
parent 5ebb057828
commit 1e7725a4a0
1 changed files with 34 additions and 31 deletions

View File

@ -422,41 +422,44 @@ class EditProfileActivity : BaseActivity() {
override fun doInBackground(vararg uris: Uri): Boolean? { override fun doInBackground(vararg uris: Uri): Boolean? {
val uri = uris[0] val uri = uris[0]
val inputStream: InputStream? val inputStream: InputStream?
try { try {
inputStream = contentResolver.openInputStream(uri) inputStream = contentResolver.openInputStream(uri)
} catch (e: FileNotFoundException) { } catch (e: FileNotFoundException) {
Log.d(TAG, Log.getStackTraceString(e)) Log.d(TAG, Log.getStackTraceString(e))
return false return false
} }
val sourceBitmap: Bitmap? val sourceBitmap: Bitmap?
try { try {
sourceBitmap = BitmapFactory.decodeStream(inputStream, null, null) sourceBitmap = BitmapFactory.decodeStream(inputStream, null, null)
} catch (error: OutOfMemoryError) { } catch (error: OutOfMemoryError) {
Log.d(TAG, Log.getStackTraceString(error)) Log.d(TAG, Log.getStackTraceString(error))
return false return false
} finally { } finally {
IOUtils.closeQuietly(inputStream) IOUtils.closeQuietly(inputStream)
} }
if (sourceBitmap == null) { if (sourceBitmap == null) {
return false return false
} }
val bitmap = Bitmap.createScaledBitmap(sourceBitmap, resizeWidth, resizeHeight, true)
sourceBitmap.recycle()
if (bitmap == null) {
return false
}
resultBitmap = bitmap //dont upscale image if its smaller than the desired size
val bitmap =
if(sourceBitmap.width <= resizeWidth && sourceBitmap.height <= resizeHeight) {
sourceBitmap
} else {
Bitmap.createScaledBitmap(sourceBitmap, resizeWidth, resizeHeight, true)
}
if (!saveBitmapToFile(bitmap, cacheFile)) { resultBitmap = bitmap
return false
}
if (isCancelled) { if (!saveBitmapToFile(bitmap, cacheFile)) {
return false return false
} }
if (isCancelled) {
return false
}
return true return true
} }