Skip to content

Commit e36fc8f

Browse files
committed
#96 Fix picking camera image without cropping
1 parent 62c1e60 commit e36fc8f

File tree

8 files changed

+54
-69
lines changed

8 files changed

+54
-69
lines changed

imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/ImagePickerActivity.kt

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,7 @@ class ImagePickerActivity : AppCompatActivity() {
143143
* Dispatch incoming result to the correct provider.
144144
*/
145145
override fun onRequestPermissionsResult(
146-
requestCode: Int,
147-
permissions: Array<String>,
148-
grantResults: IntArray
146+
requestCode: Int, permissions: Array<String>, grantResults: IntArray
149147
) {
150148
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
151149
mCameraProvider?.onRequestPermissionsResult(requestCode)
@@ -170,8 +168,7 @@ class ImagePickerActivity : AppCompatActivity() {
170168
outputFormat = mCropProvider.outputFormat()
171169
)
172170
mCompressionProvider.isResizeRequired(uri) -> mCompressionProvider.compress(
173-
uri = uri,
174-
outputFormat = mCropProvider.outputFormat()
171+
uri = uri, outputFormat = mCropProvider.outputFormat()
175172
)
176173
else -> setResult(uri)
177174
}
@@ -180,7 +177,7 @@ class ImagePickerActivity : AppCompatActivity() {
180177
fun setMultipleImage(fileList: ArrayList<Uri>) {
181178
this.fileToCrop = fileList
182179

183-
if (!fileList.isNullOrEmpty()) {
180+
if (fileList.isNotEmpty()) {
184181
val file = fileList[0]
185182
setMultipleCropper(uri = file)
186183
try {
@@ -203,8 +200,7 @@ class ImagePickerActivity : AppCompatActivity() {
203200
outputFormat = mCropProvider.outputFormat()
204201
)
205202
mCompressionProvider.isResizeRequired(uri) -> mCompressionProvider.compress(
206-
uri = uri,
207-
outputFormat = mCropProvider.outputFormat()
203+
uri = uri, outputFormat = mCropProvider.outputFormat()
208204
)
209205
}
210206
}
@@ -255,17 +251,14 @@ class ImagePickerActivity : AppCompatActivity() {
255251
mCameraProvider?.let {
256252
// Delete Camera file after Compress. Else there will be two image for the same action.
257253
// In case of Gallery Provider, we will get original image path, so we will not delete that.
258-
file.delete()
259-
// it.delete()
260-
}
261-
262-
// If crop file is not null, Delete it after crop
263-
mCropUri?.path?.let {
264-
File(it).delete()
254+
mImageUri?.path?.let { path ->
255+
File(path).delete()
256+
}
257+
it.delete()
258+
mImageUri = null
265259
}
266-
mCropUri = null
267260

268-
setResult(mCropUri!!)
261+
setResult(Uri.fromFile(file))
269262
}
270263

271264
/**
@@ -308,4 +301,4 @@ class ImagePickerActivity : AppCompatActivity() {
308301
finish()
309302
}
310303

311-
}
304+
}

imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/BaseProvider.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,18 @@ abstract class BaseProvider(protected val activity: ImagePickerActivity) :
5555
/**
5656
* This method will be Call on Error, It can be used for clean up Tasks
5757
*/
58-
protected open fun onFailure() {
59-
}
58+
protected open fun onFailure() {}
6059

6160
/**
6261
* Save all appropriate provider state.
6362
*/
64-
open fun onSaveInstanceState(outState: Bundle) {
65-
}
63+
open fun onSaveInstanceState(outState: Bundle) {}
6664

6765
/**
6866
* Restores the saved state for all Providers.
6967
*
7068
* @param savedInstanceState the Bundle returned by {@link #onSaveInstanceState()}
7169
* @see #onSaveInstanceState()
7270
*/
73-
open fun onRestoreInstanceState(savedInstanceState: Bundle?) {
74-
}
71+
open fun onRestoreInstanceState(savedInstanceState: Bundle?) {}
7572
}

imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/CameraProvider.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ class CameraProvider(
3939
/**
4040
* Permission Require for Image Capture using Camera
4141
*/
42-
private val REQUIRED_PERMISSIONS = arrayOf(
43-
Manifest.permission.CAMERA
44-
)
42+
private val REQUIRED_PERMISSIONS = arrayOf(Manifest.permission.CAMERA)
4543

4644
private const val PERMISSION_INTENT_REQ_CODE = 4282
4745
}
@@ -200,4 +198,4 @@ class CameraProvider(
200198
}.toTypedArray()
201199
}
202200

203-
}
201+
}

imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/CompressionProvider.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import com.github.drjacky.imagepicker.ImagePicker
1010
import com.github.drjacky.imagepicker.ImagePickerActivity
1111
import com.github.drjacky.imagepicker.util.ExifDataCopier
1212
import com.github.drjacky.imagepicker.util.FileUriUtils
13+
import com.github.drjacky.imagepicker.util.ImageUtil.getBitmap
1314
import kotlinx.coroutines.launch
1415
import java.io.File
1516
import java.io.FileOutputStream
17+
import java.util.*
1618

1719
/**
1820
* Compress Selected/Captured Image
@@ -66,8 +68,8 @@ class CompressionProvider(activity: ImagePickerActivity) : BaseProvider(activity
6668
}
6769

6870
private fun compressTask(uri: Uri, outputFormat: Bitmap.CompressFormat?): File? {
69-
var bitmap = BitmapFactory.decodeFile(uri.path, BitmapFactory.Options())
70-
if (maxWidth > 0L && maxHeight > 0L) {
71+
var bitmap = getBitmap(activity, uri)
72+
if (bitmap != null && maxWidth > 0L && maxHeight > 0L) {
7173
// resize if desired
7274
bitmap = if ((bitmap.width > maxWidth || bitmap.height > maxHeight) && keepRatio) {
7375
var width = maxWidth
@@ -90,15 +92,15 @@ class CompressionProvider(activity: ImagePickerActivity) : BaseProvider(activity
9092
val format = outputFormat ?: FileUriUtils.getImageExtensionFormat(baseContext, uri)
9193
var out: FileOutputStream? = null
9294
return try {
93-
val temp = "temp.${format.name}"
95+
val temp = "temp${Random().nextInt()}.${format.name}"
9496
val tempPath = activity.filesDir.toString() + "/$temp"
9597
with(File(tempPath)) {
9698
if (exists()) {
9799
delete()
98100
}
99101
}
100102
out = activity.openFileOutput(temp, Context.MODE_PRIVATE)
101-
bitmap.compress(format, 100, out)
103+
bitmap?.compress(format, 100, out)
102104
File(tempPath)
103105
} catch (e: Exception) {
104106
e.printStackTrace()
@@ -121,4 +123,4 @@ class CompressionProvider(activity: ImagePickerActivity) : BaseProvider(activity
121123
BitmapFactory.decodeStream(contentResolver.openInputStream(uri), null, options)
122124
return intArrayOf(options.outWidth, options.outHeight)
123125
}
124-
}
126+
}

imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/CropProvider.kt

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
package com.github.drjacky.imagepicker.provider
22

33
import android.app.Activity
4-
import android.content.Context
54
import android.content.Intent
65
import android.graphics.Bitmap
7-
import android.graphics.BitmapFactory
8-
import android.graphics.ImageDecoder
96
import android.net.Uri
10-
import android.os.Build
117
import android.os.Bundle
128
import android.os.Environment
139
import androidx.activity.result.ActivityResult
@@ -17,6 +13,7 @@ import com.github.drjacky.imagepicker.ImagePickerActivity
1713
import com.github.drjacky.imagepicker.R
1814
import com.github.drjacky.imagepicker.util.FileUriUtils
1915
import com.github.drjacky.imagepicker.util.FileUtil.getCompressFormat
16+
import com.github.drjacky.imagepicker.util.ImageUtil.getBitmap
2017
import com.yalantis.ucrop.UCrop
2118
import java.io.ByteArrayOutputStream
2219
import java.io.File
@@ -213,24 +210,6 @@ class CropProvider(activity: ImagePickerActivity, private val launcher: (Intent)
213210
}
214211
}
215212

216-
private fun getBitmap(context: Context, imageUri: Uri): Bitmap? {
217-
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
218-
try {
219-
ImageDecoder.decodeBitmap(
220-
ImageDecoder.createSource(context.contentResolver, imageUri)
221-
)
222-
} catch (e: ImageDecoder.DecodeException) {
223-
null
224-
}
225-
} else {
226-
context
227-
.contentResolver
228-
.openInputStream(imageUri)?.use { inputStream ->
229-
BitmapFactory.decodeStream(inputStream)
230-
}
231-
}
232-
}
233-
234213
@Throws(IOException::class)
235214
private fun convertBitmapToFile(destinationFile: File, bitmap: Bitmap, extension: String) {
236215
destinationFile.createNewFile()
@@ -262,4 +241,4 @@ class CropProvider(activity: ImagePickerActivity, private val launcher: (Intent)
262241
cropImageUri = null
263242
}
264243

265-
}
244+
}

imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/provider/GalleryProvider.kt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ import java.io.IOException
2626
class GalleryProvider(
2727
activity: ImagePickerActivity,
2828
private val launcher: (Intent) -> Unit
29-
) :
30-
BaseProvider(activity) {
29+
) : BaseProvider(activity) {
3130

3231
private var fileList: ArrayList<Uri>? = null
3332

@@ -46,13 +45,9 @@ class GalleryProvider(
4645
Manifest.permission.WRITE_EXTERNAL_STORAGE
4746
)
4847
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
49-
arrayOf(
50-
Manifest.permission.READ_MEDIA_IMAGES
51-
)
48+
arrayOf(Manifest.permission.READ_MEDIA_IMAGES)
5249
} else {
53-
arrayOf(
54-
Manifest.permission.READ_EXTERNAL_STORAGE
55-
)
50+
arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE)
5651
}
5752

5853
private const val PERMISSION_INTENT_REQ_CODE = 4262
@@ -162,7 +157,7 @@ class GalleryProvider(
162157
*/
163158
private fun handleResult(data: Intent?) {
164159
data?.clipData?.let { dataClipData ->
165-
fileList = ArrayList<Uri>()
160+
fileList = ArrayList()
166161
for (i in 0 until dataClipData.itemCount) {
167162
val uri = dataClipData.getItemAt(i).uri
168163
fileList!!.add(uri)
@@ -192,4 +187,4 @@ class GalleryProvider(
192187
}
193188
}
194189
}
195-
}
190+
}

imagepicker/src/main/kotlin/com/github/drjacky/imagepicker/util/ImageUtil.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package com.github.drjacky.imagepicker.util
1717

18+
import android.content.Context
1819
import android.graphics.*
20+
import android.net.Uri
1921
import android.os.Build
2022
import java.io.File
2123
import java.io.FileOutputStream
@@ -211,4 +213,23 @@ object ImageUtil {
211213
else -> 1
212214
}
213215
}
214-
}
216+
217+
fun getBitmap(context: Context, imageUri: Uri): Bitmap? {
218+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
219+
try {
220+
ImageDecoder.decodeBitmap(
221+
ImageDecoder.createSource(context.contentResolver, imageUri)
222+
)
223+
} catch (e: ImageDecoder.DecodeException) {
224+
e.printStackTrace()
225+
null
226+
}
227+
} else {
228+
context
229+
.contentResolver
230+
.openInputStream(imageUri)?.use { inputStream ->
231+
BitmapFactory.decodeStream(inputStream)
232+
}
233+
}
234+
}
235+
}

sample/src/main/kotlin/com.github.drjacky.imagepicker/sample/MainActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class MainActivity : AppCompatActivity() {
140140
fun pickCameraImage(view: View) {
141141
cameraLauncher.launch(
142142
ImagePicker.with(this)
143-
.crop()
143+
//.crop()
144144
.cameraOnly()
145145
.maxResultSize(1080, 1920, true)
146146
.createIntent()
@@ -174,4 +174,4 @@ class MainActivity : AppCompatActivity() {
174174
.setPositiveButton("Ok", null)
175175
.show()
176176
}
177-
}
177+
}

0 commit comments

Comments
 (0)