Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

136 fix get fragmentactivity from contextwrapper #143

Merged
merged 4 commits into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [unreleased x.x.x] -

## [3.1.3] - 10/06/21
### Fixed
- ContextWrapper cannot be cast to FragmentActivity [#136](https://github.com/CanHub/Android-Image-Cropper/issues/136)

## [3.1.2] - 07/06/21
### Fixed
- Missing file extension under Android 10 [#138](https://github.com/CanHub/Android-Image-Cropper/issues/138)
Expand Down
142 changes: 29 additions & 113 deletions cropper/src/main/java/com/canhub/cropper/BitmapCroppingWorkerJob.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.canhub.cropper

import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import androidx.annotation.Nullable
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.lang.ref.WeakReference
import kotlin.coroutines.CoroutineContext

class BitmapCroppingWorkerJob internal constructor(
private val activity: FragmentActivity,
class BitmapCroppingWorkerJob(
private val context: Context,
private val cropImageViewReference: WeakReference<CropImageView>,
val uri: Uri?,
private val bitmap: Bitmap?,
Expand All @@ -32,99 +32,21 @@ class BitmapCroppingWorkerJob internal constructor(
private val saveUri: Uri?,
private val saveCompressFormat: Bitmap.CompressFormat? = Bitmap.CompressFormat.JPEG,
private val saveCompressQuality: Int
) {

private var currentJob: Job? = null

constructor(
activity: FragmentActivity,
cropImageView: CropImageView,
bitmap: Bitmap?,
cropPoints: FloatArray,
degreesRotated: Int,
fixAspectRatio: Boolean,
aspectRatioX: Int,
aspectRatioY: Int,
reqWidth: Int,
reqHeight: Int,
flipHorizontally: Boolean,
flipVertically: Boolean,
options: CropImageView.RequestSizeOptions,
saveUri: Uri?,
@Nullable saveCompressFormat: Bitmap.CompressFormat?,
saveCompressQuality: Int
) : this(
activity = activity,
cropImageViewReference = WeakReference(cropImageView),
uri = null,
bitmap = bitmap,
cropPoints = cropPoints,
degreesRotated = degreesRotated,
orgWidth = 0,
orgHeight = 0,
fixAspectRatio = fixAspectRatio,
aspectRatioX = aspectRatioX,
aspectRatioY = aspectRatioY,
reqWidth = reqWidth,
reqHeight = reqHeight,
flipHorizontally = flipHorizontally,
flipVertically = flipVertically,
options = options,
saveUri = saveUri,
saveCompressFormat = saveCompressFormat ?: Bitmap.CompressFormat.JPEG,
saveCompressQuality = saveCompressQuality
)

constructor(
activity: FragmentActivity,
cropImageView: CropImageView,
uri: Uri?,
cropPoints: FloatArray,
degreesRotated: Int,
orgWidth: Int,
orgHeight: Int,
fixAspectRatio: Boolean,
aspectRatioX: Int,
aspectRatioY: Int,
reqWidth: Int,
reqHeight: Int,
flipHorizontally: Boolean,
flipVertically: Boolean,
options: CropImageView.RequestSizeOptions,
saveUri: Uri?,
@Nullable saveCompressFormat: Bitmap.CompressFormat,
saveCompressQuality: Int
) : this(
activity = activity,
cropImageViewReference = WeakReference(cropImageView),
uri = uri,
bitmap = null,
cropPoints = cropPoints,
degreesRotated = degreesRotated,
orgWidth = orgWidth,
orgHeight = orgHeight,
fixAspectRatio = fixAspectRatio,
aspectRatioX = aspectRatioX,
aspectRatioY = aspectRatioY,
reqWidth = reqWidth,
reqHeight = reqHeight,
flipHorizontally = flipHorizontally,
flipVertically = flipVertically,
options = options,
saveUri = saveUri,
saveCompressFormat = saveCompressFormat,
saveCompressQuality = saveCompressQuality
)
) : CoroutineScope {

private var job: Job = Job()
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job

fun start() {
currentJob = activity.lifecycleScope.launch(Dispatchers.Default) {
job = launch(Dispatchers.Default) {
try {
if (isActive) {
val bitmapSampled: BitmapUtils.BitmapSampled
when {
uri != null -> {
bitmapSampled = BitmapUtils.cropBitmap(
activity,
context,
uri,
cropPoints,
degreesRotated,
Expand Down Expand Up @@ -159,29 +81,25 @@ class BitmapCroppingWorkerJob internal constructor(
val resizedBitmap =
BitmapUtils.resizeBitmap(bitmapSampled.bitmap, reqWidth, reqHeight, options)

if (saveUri == null) {
onPostExecute(
Result(
if (saveUri == null)
onPostExecute(Result(resizedBitmap, bitmapSampled.sampleSize))
else
launch(Dispatchers.IO) {
BitmapUtils.writeBitmapToUri(
context,
resizedBitmap,
bitmapSampled.sampleSize
)
)
} else {
BitmapUtils.writeBitmapToUri(
activity,
resizedBitmap,
saveUri,
saveCompressFormat ?: Bitmap.CompressFormat.JPEG,
saveCompressQuality
)
resizedBitmap.recycle()
onPostExecute(
Result(
saveUri,
bitmapSampled.sampleSize
saveCompressFormat ?: Bitmap.CompressFormat.JPEG,
saveCompressQuality
)
)
}
resizedBitmap.recycle()
onPostExecute(
Result(
saveUri,
bitmapSampled.sampleSize
)
)
}
}
} catch (e: Exception) {
onPostExecute(Result(e, saveUri != null))
Expand All @@ -206,11 +124,9 @@ class BitmapCroppingWorkerJob internal constructor(
}

fun cancel() {
currentJob?.cancel()
job.cancel()
}

// region: Inner class: Result
companion object
class Result {

/** The cropped bitmap */
Expand Down
33 changes: 22 additions & 11 deletions cropper/src/main/java/com/canhub/cropper/BitmapLoadingWorkerJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@ package com.canhub.cropper
import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import com.canhub.cropper.utils.getFilePathFromUri
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.lang.ref.WeakReference
import kotlin.coroutines.CoroutineContext

class BitmapLoadingWorkerJob internal constructor(
private val activity: FragmentActivity,
private val context: Context,
cropImageView: CropImageView,
val uri: Uri
) {
) : CoroutineScope {

private val width: Int
private val height: Int
private val cropImageViewReference = WeakReference(cropImageView)
private var currentJob: Job? = null
private var currentJob: Job = Job()
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + currentJob

init {
val metrics = cropImageView.resources.displayMetrics
Expand All @@ -31,15 +34,22 @@ class BitmapLoadingWorkerJob internal constructor(
}

fun start() {
currentJob = activity.lifecycleScope.launch(Dispatchers.Default) {
currentJob = launch(Dispatchers.Default) {
try {
if (isActive) {
val decodeResult =
BitmapUtils.decodeSampledBitmap(activity, uri, width, height)
BitmapUtils.decodeSampledBitmap(context, uri, width, height)
if (isActive) {
val rotateResult =
BitmapUtils.rotateBitmapByExif(decodeResult.bitmap, activity, uri)
onPostExecute(Result(uri, rotateResult.bitmap, decodeResult.sampleSize, rotateResult.degrees))
BitmapUtils.rotateBitmapByExif(decodeResult.bitmap, context, uri)
onPostExecute(
Result(
uri = uri,
bitmap = rotateResult.bitmap,
loadSampleSize = decodeResult.sampleSize,
degreesRotated = rotateResult.degrees
)
)
}
}
} catch (e: Exception) {
Expand Down Expand Up @@ -70,11 +80,12 @@ class BitmapLoadingWorkerJob internal constructor(
}

fun cancel() {
currentJob?.cancel()
currentJob.cancel()
}

/** The result of BitmapLoadingWorkerJob async loading. */
companion object class Result {
companion object
class Result {

/**
* The Android URI of the image to load.
Expand Down
75 changes: 39 additions & 36 deletions cropper/src/main/java/com/canhub/cropper/CropImageView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.ProgressBar
import androidx.exifinterface.media.ExifInterface
import androidx.fragment.app.FragmentActivity
import com.canhub.cropper.CropOverlayView.CropWindowChangeListener
import com.canhub.cropper.utils.getFilePathFromUri
import java.lang.ref.WeakReference
Expand Down Expand Up @@ -731,7 +730,7 @@ class CropImageView @JvmOverloads constructor(context: Context, attrs: Attribute
clearImageInt()
mCropOverlayView!!.initialCropWindowRect = null
mBitmapLoadingWorkerJob =
WeakReference(BitmapLoadingWorkerJob((context as FragmentActivity), this, uri))
WeakReference(BitmapLoadingWorkerJob(context, this, uri))
mBitmapLoadingWorkerJob!!.get()!!.start()
setProgressBarVisibility()
}
Expand Down Expand Up @@ -999,45 +998,49 @@ class CropImageView @JvmOverloads constructor(context: Context, attrs: Attribute
) {
WeakReference(
BitmapCroppingWorkerJob(
(context as FragmentActivity),
this,
imageUri,
cropPoints,
mDegreesRotated,
orgWidth,
orgHeight,
mCropOverlayView!!.isFixAspectRatio,
mCropOverlayView.aspectRatioX,
mCropOverlayView.aspectRatioY,
newReqWidth,
newReqHeight,
mFlipHorizontally,
mFlipVertically,
options,
saveUri,
saveCompressFormat,
saveCompressQuality
context = context,
cropImageViewReference = WeakReference(this),
uri = imageUri,
bitmap = null,
cropPoints = cropPoints,
degreesRotated = mDegreesRotated,
orgWidth = orgWidth,
orgHeight = orgHeight,
fixAspectRatio = mCropOverlayView!!.isFixAspectRatio,
aspectRatioX = mCropOverlayView.aspectRatioX,
aspectRatioY = mCropOverlayView.aspectRatioY,
reqWidth = newReqWidth,
reqHeight = newReqHeight,
flipHorizontally = mFlipHorizontally,
flipVertically = mFlipVertically,
options = options,
saveUri = saveUri,
saveCompressFormat = saveCompressFormat,
saveCompressQuality = saveCompressQuality
)
)
} else {
WeakReference(
BitmapCroppingWorkerJob(
(context as FragmentActivity),
this,
bitmap,
cropPoints,
mDegreesRotated,
mCropOverlayView!!.isFixAspectRatio,
mCropOverlayView.aspectRatioX,
mCropOverlayView.aspectRatioY,
newReqWidth,
newReqHeight,
mFlipHorizontally,
mFlipVertically,
options,
saveUri,
saveCompressFormat,
saveCompressQuality
context = context,
cropImageViewReference = WeakReference(this),
uri = null,
bitmap = bitmap,
cropPoints = cropPoints,
degreesRotated = mDegreesRotated,
orgWidth = 0,
orgHeight = 0,
fixAspectRatio = mCropOverlayView!!.isFixAspectRatio,
aspectRatioX = mCropOverlayView.aspectRatioX,
aspectRatioY = mCropOverlayView.aspectRatioY,
reqWidth = newReqWidth,
reqHeight = newReqHeight,
flipHorizontally = mFlipHorizontally,
flipVertically = mFlipVertically,
options = options,
saveUri = saveUri,
saveCompressFormat = saveCompressFormat,
saveCompressQuality = saveCompressQuality
)
)
}
Expand Down
2 changes: 1 addition & 1 deletion versions.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ext {

// Project
libVersion = "3.1.2"
libVersion = "3.1.3"
compileSdkVersion = 30
targetSdkVersion = 30
minSdkVersion = 14
Expand Down