Skip to content

Commit

Permalink
WIP clean uri need
Browse files Browse the repository at this point in the history
  • Loading branch information
Canato committed Aug 16, 2021
1 parent 0791bd1 commit 7bb8ab3
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 212 deletions.
35 changes: 15 additions & 20 deletions cropper/src/main/java/com/canhub/cropper/BitmapCroppingWorkerJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class BitmapCroppingWorkerJob(
private val flipHorizontally: Boolean,
private val flipVertically: Boolean,
private val options: CropImageView.RequestSizeOptions,
private val saveUri: Uri?,
private val saveCompressFormat: Bitmap.CompressFormat? = Bitmap.CompressFormat.JPEG,
private val saveCompressQuality: Int
) : CoroutineScope {
Expand Down Expand Up @@ -81,28 +80,24 @@ class BitmapCroppingWorkerJob(
val resizedBitmap =
BitmapUtils.resizeBitmap(bitmapSampled.bitmap, reqWidth, reqHeight, options)

if (saveUri == null)
onPostExecute(Result(resizedBitmap, bitmapSampled.sampleSize))
else
launch(Dispatchers.IO) {
val newUri = BitmapUtils.writeBitmapToUri(
context,
resizedBitmap,
saveUri,
saveCompressFormat ?: Bitmap.CompressFormat.JPEG,
saveCompressQuality
)
resizedBitmap.recycle()
onPostExecute(
Result(
newUri, //saveUri,
bitmapSampled.sampleSize
)
launch(Dispatchers.IO) {
val newUri = BitmapUtils.writeBitmapToUri(
context,
resizedBitmap,
saveCompressFormat ?: Bitmap.CompressFormat.JPEG,
saveCompressQuality
)
resizedBitmap.recycle()
onPostExecute(
Result(
newUri, //saveUri,
bitmapSampled.sampleSize
)
}
)
}
}
} catch (e: Exception) {
onPostExecute(Result(e, saveUri != null))
onPostExecute(Result(e, false))
}
}
}
Expand Down
30 changes: 5 additions & 25 deletions cropper/src/main/java/com/canhub/cropper/BitmapUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ internal object BitmapUtils {
val EMPTY_RECT = Rect()
val EMPTY_RECT_F = RectF()
private const val IMAGE_MAX_BITMAP_DIMENSION = 2048
private const val WRITE_AND_TRUNCATE = "wt"

/**
* Reusable rectangle for general internal usage
Expand Down Expand Up @@ -396,28 +397,9 @@ internal object BitmapUtils {
* @return the uri where the image was saved in, either the given uri or new pointing to temp
* file.
*/
fun writeTempStateStoreBitmap(context: Context, bitmap: Bitmap?, uri: Uri?): Uri? {
var tempUri = uri
return try {
var needSave = true
if (tempUri == null) {
// We have this because of a HUAWEI path bug when we use getUriForFile
tempUri = if (isAtLeastQ29()) {
FileProvider.getUriForFile(
context,
context.packageName + CommonValues.authority,
File.createTempFile("aic_state_store_temp", ".jpg", context.cacheDir)
)
} else {
Uri.fromFile(
File.createTempFile("aic_state_store_temp", ".jpg", context.cacheDir)
)
}
} else if (tempUri.path?.let { File(it).exists() } == true) {
needSave = false
}
if (needSave) writeBitmapToUri(context, bitmap!!, tempUri, CompressFormat.JPEG, 95)
else tempUri
fun writeTempStateStoreBitmap(context: Context, bitmap: Bitmap?): Uri? =
try {
writeBitmapToUri(context, bitmap!!, CompressFormat.JPEG, 95)
} catch (e: Exception) {
Log.w(
"AIC",
Expand All @@ -426,7 +408,6 @@ internal object BitmapUtils {
)
null
}
}

/**
* Write the given bitmap to the given uri using the given compression.
Expand All @@ -435,14 +416,13 @@ internal object BitmapUtils {
fun writeBitmapToUri(
context: Context,
bitmap: Bitmap,
uri: Uri?,
compressFormat: CompressFormat?,
compressQuality: Int
): Uri? {
val newUri = buildUri(context, compressFormat)
var outputStream: OutputStream? = null
try {
outputStream = context.contentResolver.openOutputStream(newUri!!)
outputStream = context.contentResolver.openOutputStream(newUri!!, WRITE_AND_TRUNCATE)

bitmap.compress(compressFormat ?: CompressFormat.JPEG, compressQuality, outputStream)
} finally {
Expand Down
37 changes: 0 additions & 37 deletions cropper/src/main/java/com/canhub/cropper/CropImageActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ open class CropImageActivity :
open fun cropImage() {
if (options.noOutputImage) setResult(null, null, 1)
else cropImageView?.croppedImageAsync(
outputUri,
options.outputCompressFormat,
options.outputCompressQuality,
options.outputRequestWidth,
Expand All @@ -257,42 +256,6 @@ open class CropImageActivity :
cropImageView?.rotateImage(degrees)
}

/**
* Get Android uri to save the cropped image into.<br></br>
* Use the given in options or create a temp file.
*/
val outputUri: Uri?
get() {
var outputUri = options.outputUri
if (outputUri == null || outputUri == Uri.EMPTY) {
outputUri = try {
val ext = when (options.outputCompressFormat) {
Bitmap.CompressFormat.JPEG -> ".jpg"
Bitmap.CompressFormat.PNG -> ".png"
else -> ".webp"
}
// We have this because of a HUAWEI path bug when we use getUriForFile
if (CommonVersionCheck.isAtLeastQ29()) {
try {
val file = File.createTempFile(
"cropped",
ext,
getExternalFilesDir(Environment.DIRECTORY_PICTURES)
)
getUriForFile(applicationContext, file)
} catch (e: Exception) {
Log.e("AIC", "${e.message}")
val file = File.createTempFile("cropped", ext, cacheDir)
getUriForFile(applicationContext, file)
}
} else Uri.fromFile(File.createTempFile("cropped", ext, cacheDir))
} catch (e: IOException) {
throw RuntimeException("Failed to create temp file for output image", e)
}
}
return outputUri
}

/**
* Result with cropped image data or error if failed.
*/
Expand Down
Loading

0 comments on commit 7bb8ab3

Please sign in to comment.