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

fix deprecated pickCamera #408

Merged
merged 2 commits into from
Oct 23, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import android.graphics.PorterDuff
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.ActionBar
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
Expand All @@ -21,10 +23,6 @@ import com.esafirm.imagepicker.model.Image

class ImagePickerActivity : AppCompatActivity(), ImagePickerInteractionListener {

companion object {
private const val RC_CAMERA = 1011
}

private val cameraModule = ImagePickerComponentsHolder.cameraModule

private var actionBar: ActionBar? = null
Expand All @@ -40,6 +38,28 @@ class ImagePickerActivity : AppCompatActivity(), ImagePickerInteractionListener

private val isCameraOnly by lazy { cameraOnlyConfig != null }

private val startForCameraResult = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result: ActivityResult ->
if (result.resultCode == Activity.RESULT_OK) {
val intent = result.data
// Handle the Intent
if (intent?.extras?.isEmpty == true) {
cameraModule.removeImage(this)
setResult(RESULT_CANCELED)
finish()
} else {
cameraModule.getImage(this, intent) { images ->
finishPickImages(ImagePickerUtils.createResultIntent(images))
}
}
} else {
cameraModule.removeImage(this)
setResult(RESULT_CANCELED)
finish()
}
}

override fun attachBaseContext(newBase: Context) {
super.attachBaseContext(LocaleManager.updateResources(newBase))
}
Expand All @@ -56,7 +76,7 @@ class ImagePickerActivity : AppCompatActivity(), ImagePickerInteractionListener

if (isCameraOnly) {
val cameraIntent = cameraModule.getCameraIntent(this, cameraOnlyConfig!!)
startActivityForResult(cameraIntent, RC_CAMERA)
startForCameraResult.launch(cameraIntent)
return
}

Expand Down Expand Up @@ -121,7 +141,7 @@ class ImagePickerActivity : AppCompatActivity(), ImagePickerInteractionListener
if (!imagePickerFragment.handleBack()) {
super.onBackPressed()
}
}else {
} else {
super.onBackPressed()
}
}
Expand All @@ -142,22 +162,6 @@ class ImagePickerActivity : AppCompatActivity(), ImagePickerInteractionListener
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_CANCELED) {
cameraModule.removeImage(this)
setResult(RESULT_CANCELED)
finish()
return
}
if (requestCode == RC_CAMERA && resultCode == Activity.RESULT_OK) {
cameraModule.getImage(this, data) { images ->
val result = ImagePickerUtils.createResultIntent(images)
finishPickImages(result)
}
}
}

/* --------------------------------------------------- */
/* > ImagePickerInteractionListener Methods */
/* --------------------------------------------------- */
Expand All @@ -179,4 +183,4 @@ class ImagePickerActivity : AppCompatActivity(), ImagePickerInteractionListener
setResult(RESULT_OK, result)
finish()
}
}
}