Skip to content

Commit

Permalink
Clean uri need
Browse files Browse the repository at this point in the history
  • Loading branch information
Canato committed Sep 1, 2021
1 parent e9b339a commit 95d1484
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 234 deletions.
2 changes: 1 addition & 1 deletion cropper/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ android {
}

dependencies {
api "androidx.appcompat:appcompat:$androidXAppCompatVersion"
implementation "androidx.appcompat:appcompat:$androidXAppCompatVersion"

implementation "androidx.activity:activity-ktx:$androidXActivity"

Expand Down
37 changes: 16 additions & 21 deletions cropper/src/main/java/com/canhub/cropper/BitmapCroppingWorkerJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ 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 saveCompressFormat: Bitmap.CompressFormat,
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,
saveCompressQuality
)
resizedBitmap.recycle()
onPostExecute(
Result(
newUri, // saveUri,
bitmapSampled.sampleSize
)
}
)
}
}
} catch (e: Exception) {
onPostExecute(Result(e, saveUri != null))
onPostExecute(Result(e, false))
}
}
}
Expand Down
43 changes: 10 additions & 33 deletions cropper/src/main/java/com/canhub/cropper/BitmapUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import android.net.Uri
import android.os.Environment
import android.util.Log
import android.util.Pair
import androidx.core.content.FileProvider
import androidx.exifinterface.media.ExifInterface
import com.canhub.cropper.CropImageView.RequestSizeOptions
import com.canhub.cropper.common.CommonValues
import com.canhub.cropper.common.CommonVersionCheck.isAtLeastQ29
import com.canhub.cropper.utils.getUriForFile
import java.io.Closeable
Expand Down Expand Up @@ -44,6 +42,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 @@ -392,32 +391,12 @@ internal object BitmapUtils {
* Write given bitmap to a temp file. If file already exists no-op as we already saved the file in
* this session. Uses JPEG 95% compression.
*
* @param uri the uri to write the bitmap to, if null
* @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 +405,6 @@ internal object BitmapUtils {
)
null
}
}

/**
* Write the given bitmap to the given uri using the given compression.
Expand All @@ -435,16 +413,15 @@ internal object BitmapUtils {
fun writeBitmapToUri(
context: Context,
bitmap: Bitmap,
uri: Uri?,
compressFormat: CompressFormat?,
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)
bitmap.compress(compressFormat, compressQuality, outputStream)
} finally {
closeSafe(outputStream)
}
Expand All @@ -453,7 +430,7 @@ internal object BitmapUtils {

private fun buildUri(
context: Context,
compressFormat: CompressFormat?
compressFormat: CompressFormat
): Uri? =
try {
val ext = when (compressFormat) {
Expand Down Expand Up @@ -740,8 +717,8 @@ internal object BitmapUtils {
options.inSampleSize = (
sampleMulti
* calculateInSampleSizeByReqestedSize(
rect.width(), rect.height(), reqWidth, reqHeight
)
rect.width(), rect.height(), reqWidth, reqHeight
)
)
stream = context.contentResolver.openInputStream(uri)
decoder = BitmapRegionDecoder.newInstance(stream, false)
Expand Down
42 changes: 0 additions & 42 deletions cropper/src/main/java/com/canhub/cropper/CropImageActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package com.canhub.cropper
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.util.Log
import android.view.Menu
import android.view.MenuItem
Expand All @@ -21,9 +19,6 @@ import com.canhub.cropper.CropImageView.OnCropImageCompleteListener
import com.canhub.cropper.CropImageView.OnSetImageUriCompleteListener
import com.canhub.cropper.common.CommonVersionCheck
import com.canhub.cropper.databinding.CropImageActivityBinding
import com.canhub.cropper.utils.getUriForFile
import java.io.File
import java.io.IOException

/**
* Built-in activity for image cropping.<br></br>
Expand Down Expand Up @@ -234,7 +229,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 +251,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 95d1484

Please sign in to comment.