From e36fc8f849a617f9b3e47d9a7f8dd9f34dac0407 Mon Sep 17 00:00:00 2001 From: ZARA Date: Fri, 12 May 2023 14:59:31 +0330 Subject: [PATCH] #96 Fix picking camera image without cropping --- .../imagepicker/ImagePickerActivity.kt | 29 +++++++------------ .../imagepicker/provider/BaseProvider.kt | 9 ++---- .../imagepicker/provider/CameraProvider.kt | 6 ++-- .../provider/CompressionProvider.kt | 12 ++++---- .../imagepicker/provider/CropProvider.kt | 25 ++-------------- .../imagepicker/provider/GalleryProvider.kt | 15 ++++------ .../drjacky/imagepicker/util/ImageUtil.kt | 23 ++++++++++++++- .../sample/MainActivity.kt | 4 +-- 8 files changed, 54 insertions(+), 69 deletions(-) diff --git a/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/ImagePickerActivity.kt b/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/ImagePickerActivity.kt index 7825a01..119ec37 100644 --- a/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/ImagePickerActivity.kt +++ b/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/ImagePickerActivity.kt @@ -143,9 +143,7 @@ class ImagePickerActivity : AppCompatActivity() { * Dispatch incoming result to the correct provider. */ override fun onRequestPermissionsResult( - requestCode: Int, - permissions: Array, - grantResults: IntArray + requestCode: Int, permissions: Array, grantResults: IntArray ) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) mCameraProvider?.onRequestPermissionsResult(requestCode) @@ -170,8 +168,7 @@ class ImagePickerActivity : AppCompatActivity() { outputFormat = mCropProvider.outputFormat() ) mCompressionProvider.isResizeRequired(uri) -> mCompressionProvider.compress( - uri = uri, - outputFormat = mCropProvider.outputFormat() + uri = uri, outputFormat = mCropProvider.outputFormat() ) else -> setResult(uri) } @@ -180,7 +177,7 @@ class ImagePickerActivity : AppCompatActivity() { fun setMultipleImage(fileList: ArrayList) { this.fileToCrop = fileList - if (!fileList.isNullOrEmpty()) { + if (fileList.isNotEmpty()) { val file = fileList[0] setMultipleCropper(uri = file) try { @@ -203,8 +200,7 @@ class ImagePickerActivity : AppCompatActivity() { outputFormat = mCropProvider.outputFormat() ) mCompressionProvider.isResizeRequired(uri) -> mCompressionProvider.compress( - uri = uri, - outputFormat = mCropProvider.outputFormat() + uri = uri, outputFormat = mCropProvider.outputFormat() ) } } @@ -255,17 +251,14 @@ class ImagePickerActivity : AppCompatActivity() { mCameraProvider?.let { // Delete Camera file after Compress. Else there will be two image for the same action. // In case of Gallery Provider, we will get original image path, so we will not delete that. - file.delete() - // it.delete() - } - - // If crop file is not null, Delete it after crop - mCropUri?.path?.let { - File(it).delete() + mImageUri?.path?.let { path -> + File(path).delete() + } + it.delete() + mImageUri = null } - mCropUri = null - setResult(mCropUri!!) + setResult(Uri.fromFile(file)) } /** @@ -308,4 +301,4 @@ class ImagePickerActivity : AppCompatActivity() { finish() } -} +} \ No newline at end of file diff --git a/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/BaseProvider.kt b/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/BaseProvider.kt index f3e279f..a0bc3b9 100644 --- a/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/BaseProvider.kt +++ b/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/BaseProvider.kt @@ -55,14 +55,12 @@ abstract class BaseProvider(protected val activity: ImagePickerActivity) : /** * This method will be Call on Error, It can be used for clean up Tasks */ - protected open fun onFailure() { - } + protected open fun onFailure() {} /** * Save all appropriate provider state. */ - open fun onSaveInstanceState(outState: Bundle) { - } + open fun onSaveInstanceState(outState: Bundle) {} /** * Restores the saved state for all Providers. @@ -70,6 +68,5 @@ abstract class BaseProvider(protected val activity: ImagePickerActivity) : * @param savedInstanceState the Bundle returned by {@link #onSaveInstanceState()} * @see #onSaveInstanceState() */ - open fun onRestoreInstanceState(savedInstanceState: Bundle?) { - } + open fun onRestoreInstanceState(savedInstanceState: Bundle?) {} } diff --git a/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/CameraProvider.kt b/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/CameraProvider.kt index c90cfc3..608a4ab 100644 --- a/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/CameraProvider.kt +++ b/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/CameraProvider.kt @@ -39,9 +39,7 @@ class CameraProvider( /** * Permission Require for Image Capture using Camera */ - private val REQUIRED_PERMISSIONS = arrayOf( - Manifest.permission.CAMERA - ) + private val REQUIRED_PERMISSIONS = arrayOf(Manifest.permission.CAMERA) private const val PERMISSION_INTENT_REQ_CODE = 4282 } @@ -200,4 +198,4 @@ class CameraProvider( }.toTypedArray() } -} +} \ No newline at end of file diff --git a/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/CompressionProvider.kt b/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/CompressionProvider.kt index 40011f8..cdab389 100644 --- a/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/CompressionProvider.kt +++ b/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/CompressionProvider.kt @@ -10,9 +10,11 @@ import com.github.drjacky.imagepicker.ImagePicker import com.github.drjacky.imagepicker.ImagePickerActivity import com.github.drjacky.imagepicker.util.ExifDataCopier import com.github.drjacky.imagepicker.util.FileUriUtils +import com.github.drjacky.imagepicker.util.ImageUtil.getBitmap import kotlinx.coroutines.launch import java.io.File import java.io.FileOutputStream +import java.util.* /** * Compress Selected/Captured Image @@ -66,8 +68,8 @@ class CompressionProvider(activity: ImagePickerActivity) : BaseProvider(activity } private fun compressTask(uri: Uri, outputFormat: Bitmap.CompressFormat?): File? { - var bitmap = BitmapFactory.decodeFile(uri.path, BitmapFactory.Options()) - if (maxWidth > 0L && maxHeight > 0L) { + var bitmap = getBitmap(activity, uri) + if (bitmap != null && maxWidth > 0L && maxHeight > 0L) { // resize if desired bitmap = if ((bitmap.width > maxWidth || bitmap.height > maxHeight) && keepRatio) { var width = maxWidth @@ -90,7 +92,7 @@ class CompressionProvider(activity: ImagePickerActivity) : BaseProvider(activity val format = outputFormat ?: FileUriUtils.getImageExtensionFormat(baseContext, uri) var out: FileOutputStream? = null return try { - val temp = "temp.${format.name}" + val temp = "temp${Random().nextInt()}.${format.name}" val tempPath = activity.filesDir.toString() + "/$temp" with(File(tempPath)) { if (exists()) { @@ -98,7 +100,7 @@ class CompressionProvider(activity: ImagePickerActivity) : BaseProvider(activity } } out = activity.openFileOutput(temp, Context.MODE_PRIVATE) - bitmap.compress(format, 100, out) + bitmap?.compress(format, 100, out) File(tempPath) } catch (e: Exception) { e.printStackTrace() @@ -121,4 +123,4 @@ class CompressionProvider(activity: ImagePickerActivity) : BaseProvider(activity BitmapFactory.decodeStream(contentResolver.openInputStream(uri), null, options) return intArrayOf(options.outWidth, options.outHeight) } -} +} \ No newline at end of file diff --git a/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/CropProvider.kt b/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/CropProvider.kt index 2b57246..341076b 100644 --- a/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/CropProvider.kt +++ b/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/CropProvider.kt @@ -1,13 +1,9 @@ package com.github.drjacky.imagepicker.provider import android.app.Activity -import android.content.Context import android.content.Intent import android.graphics.Bitmap -import android.graphics.BitmapFactory -import android.graphics.ImageDecoder import android.net.Uri -import android.os.Build import android.os.Bundle import android.os.Environment import androidx.activity.result.ActivityResult @@ -17,6 +13,7 @@ import com.github.drjacky.imagepicker.ImagePickerActivity import com.github.drjacky.imagepicker.R import com.github.drjacky.imagepicker.util.FileUriUtils import com.github.drjacky.imagepicker.util.FileUtil.getCompressFormat +import com.github.drjacky.imagepicker.util.ImageUtil.getBitmap import com.yalantis.ucrop.UCrop import java.io.ByteArrayOutputStream import java.io.File @@ -213,24 +210,6 @@ class CropProvider(activity: ImagePickerActivity, private val launcher: (Intent) } } - private fun getBitmap(context: Context, imageUri: Uri): Bitmap? { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - try { - ImageDecoder.decodeBitmap( - ImageDecoder.createSource(context.contentResolver, imageUri) - ) - } catch (e: ImageDecoder.DecodeException) { - null - } - } else { - context - .contentResolver - .openInputStream(imageUri)?.use { inputStream -> - BitmapFactory.decodeStream(inputStream) - } - } - } - @Throws(IOException::class) private fun convertBitmapToFile(destinationFile: File, bitmap: Bitmap, extension: String) { destinationFile.createNewFile() @@ -262,4 +241,4 @@ class CropProvider(activity: ImagePickerActivity, private val launcher: (Intent) cropImageUri = null } -} +} \ No newline at end of file diff --git a/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/GalleryProvider.kt b/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/GalleryProvider.kt index 0ec356c..949874d 100644 --- a/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/GalleryProvider.kt +++ b/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/GalleryProvider.kt @@ -26,8 +26,7 @@ import java.io.IOException class GalleryProvider( activity: ImagePickerActivity, private val launcher: (Intent) -> Unit -) : - BaseProvider(activity) { +) : BaseProvider(activity) { private var fileList: ArrayList? = null @@ -46,13 +45,9 @@ class GalleryProvider( Manifest.permission.WRITE_EXTERNAL_STORAGE ) } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - arrayOf( - Manifest.permission.READ_MEDIA_IMAGES - ) + arrayOf(Manifest.permission.READ_MEDIA_IMAGES) } else { - arrayOf( - Manifest.permission.READ_EXTERNAL_STORAGE - ) + arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE) } private const val PERMISSION_INTENT_REQ_CODE = 4262 @@ -162,7 +157,7 @@ class GalleryProvider( */ private fun handleResult(data: Intent?) { data?.clipData?.let { dataClipData -> - fileList = ArrayList() + fileList = ArrayList() for (i in 0 until dataClipData.itemCount) { val uri = dataClipData.getItemAt(i).uri fileList!!.add(uri) @@ -192,4 +187,4 @@ class GalleryProvider( } } } -} +} \ No newline at end of file diff --git a/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/util/ImageUtil.kt b/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/util/ImageUtil.kt index 960f933..f301b18 100644 --- a/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/util/ImageUtil.kt +++ b/imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/util/ImageUtil.kt @@ -15,7 +15,9 @@ */ package com.github.drjacky.imagepicker.util +import android.content.Context import android.graphics.* +import android.net.Uri import android.os.Build import java.io.File import java.io.FileOutputStream @@ -211,4 +213,23 @@ object ImageUtil { else -> 1 } } -} + + fun getBitmap(context: Context, imageUri: Uri): Bitmap? { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + try { + ImageDecoder.decodeBitmap( + ImageDecoder.createSource(context.contentResolver, imageUri) + ) + } catch (e: ImageDecoder.DecodeException) { + e.printStackTrace() + null + } + } else { + context + .contentResolver + .openInputStream(imageUri)?.use { inputStream -> + BitmapFactory.decodeStream(inputStream) + } + } + } +} \ No newline at end of file diff --git a/sample/src/main/kotlin/com.github.drjacky.imagepicker/sample/MainActivity.kt b/sample/src/main/kotlin/com.github.drjacky.imagepicker/sample/MainActivity.kt index 5344ea1..1808748 100644 --- a/sample/src/main/kotlin/com.github.drjacky.imagepicker/sample/MainActivity.kt +++ b/sample/src/main/kotlin/com.github.drjacky.imagepicker/sample/MainActivity.kt @@ -140,7 +140,7 @@ class MainActivity : AppCompatActivity() { fun pickCameraImage(view: View) { cameraLauncher.launch( ImagePicker.with(this) - .crop() + //.crop() .cameraOnly() .maxResultSize(1080, 1920, true) .createIntent() @@ -174,4 +174,4 @@ class MainActivity : AppCompatActivity() { .setPositiveButton("Ok", null) .show() } -} +} \ No newline at end of file