mirror of
https://github.com/SimpleMobileTools/Simple-Contacts.git
synced 2025-06-05 21:59:27 +02:00
add cropping to the selected contact photo
This commit is contained in:
@ -1,8 +1,10 @@
|
|||||||
package com.simplemobiletools.contacts.activities
|
package com.simplemobiletools.contacts.activities
|
||||||
|
|
||||||
|
import android.content.ClipData
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.graphics.drawable.ColorDrawable
|
import android.graphics.drawable.ColorDrawable
|
||||||
import android.graphics.drawable.Drawable
|
import android.graphics.drawable.Drawable
|
||||||
|
import android.net.Uri
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.provider.ContactsContract
|
import android.provider.ContactsContract
|
||||||
import android.provider.MediaStore
|
import android.provider.MediaStore
|
||||||
@ -48,6 +50,7 @@ class ContactActivity : SimpleActivity() {
|
|||||||
|
|
||||||
private val INTENT_TAKE_PHOTO = 1
|
private val INTENT_TAKE_PHOTO = 1
|
||||||
private val INTENT_CHOOSE_PHOTO = 2
|
private val INTENT_CHOOSE_PHOTO = 2
|
||||||
|
private val INTENT_CROP_PHOTO = 3
|
||||||
|
|
||||||
private val TAKE_PHOTO = 1
|
private val TAKE_PHOTO = 1
|
||||||
private val CHOOSE_PHOTO = 2
|
private val CHOOSE_PHOTO = 2
|
||||||
@ -55,7 +58,7 @@ class ContactActivity : SimpleActivity() {
|
|||||||
|
|
||||||
private var wasActivityInitialized = false
|
private var wasActivityInitialized = false
|
||||||
private var currentContactPhotoPath = ""
|
private var currentContactPhotoPath = ""
|
||||||
private var getPhotoPath = ""
|
private var lastPhotoIntentUri: Uri? = null
|
||||||
private var contact: Contact? = null
|
private var contact: Contact? = null
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
@ -156,16 +159,31 @@ class ContactActivity : SimpleActivity() {
|
|||||||
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
|
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
|
||||||
super.onActivityResult(requestCode, resultCode, resultData)
|
super.onActivityResult(requestCode, resultCode, resultData)
|
||||||
if (resultCode == RESULT_OK) {
|
if (resultCode == RESULT_OK) {
|
||||||
if (requestCode == INTENT_TAKE_PHOTO) {
|
when (requestCode) {
|
||||||
updateContactPhoto(getPhotoPath)
|
INTENT_TAKE_PHOTO, INTENT_CHOOSE_PHOTO -> startCropPhotoIntent(lastPhotoIntentUri!!)
|
||||||
} else if (requestCode == INTENT_CHOOSE_PHOTO) {
|
INTENT_CROP_PHOTO -> updateContactPhoto(lastPhotoIntentUri.toString())
|
||||||
val selectedUri = resultData?.data ?: return
|
|
||||||
val pathToUse = getRealPathFromURI(selectedUri) ?: selectedUri.toString()
|
|
||||||
updateContactPhoto(pathToUse)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun startCropPhotoIntent(uri: Uri) {
|
||||||
|
lastPhotoIntentUri = getCachePhotoUri()
|
||||||
|
Intent("com.android.camera.action.CROP").apply {
|
||||||
|
setDataAndType(uri, "image/*")
|
||||||
|
putExtra(MediaStore.EXTRA_OUTPUT, lastPhotoIntentUri)
|
||||||
|
putExtra("outputX", 720)
|
||||||
|
putExtra("outputY", 720)
|
||||||
|
putExtra("aspectX", 1)
|
||||||
|
putExtra("aspectY", 1)
|
||||||
|
putExtra("crop", "true")
|
||||||
|
putExtra("scale", "true")
|
||||||
|
putExtra("scaleUpIfNeeded", "true")
|
||||||
|
clipData = ClipData("Attachment", arrayOf("text/uri-list"), ClipData.Item(lastPhotoIntentUri))
|
||||||
|
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
|
||||||
|
startActivityForResult(this, INTENT_CROP_PHOTO)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun setupEditContact() {
|
private fun setupEditContact() {
|
||||||
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
|
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
|
||||||
supportActionBar?.title = resources.getString(R.string.edit_contact)
|
supportActionBar?.title = resources.getString(R.string.edit_contact)
|
||||||
@ -442,9 +460,8 @@ class ContactActivity : SimpleActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun startTakePhotoIntent() {
|
private fun startTakePhotoIntent() {
|
||||||
val file = getCachePhotoFile()
|
val uri = getCachePhotoUri()
|
||||||
getPhotoPath = file.absolutePath
|
lastPhotoIntentUri = uri
|
||||||
val uri = FileProvider.getUriForFile(this, "${BuildConfig.APPLICATION_ID}.provider", file)
|
|
||||||
Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply {
|
Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply {
|
||||||
putExtra(MediaStore.EXTRA_OUTPUT, uri)
|
putExtra(MediaStore.EXTRA_OUTPUT, uri)
|
||||||
if (resolveActivity(packageManager) != null) {
|
if (resolveActivity(packageManager) != null) {
|
||||||
@ -456,11 +473,12 @@ class ContactActivity : SimpleActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun startChoosePhotoIntent() {
|
private fun startChoosePhotoIntent() {
|
||||||
val file = getCachePhotoFile()
|
val uri = getCachePhotoUri()
|
||||||
val uri = FileProvider.getUriForFile(this, "${BuildConfig.APPLICATION_ID}.provider", file)
|
lastPhotoIntentUri = uri
|
||||||
Intent(Intent.ACTION_PICK).apply {
|
Intent(Intent.ACTION_PICK).apply {
|
||||||
type = "image/*"
|
type = "image/*"
|
||||||
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
clipData = ClipData("Attachment", arrayOf("text/uri-list"), ClipData.Item(uri))
|
||||||
|
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
|
||||||
putExtra(MediaStore.EXTRA_OUTPUT, uri)
|
putExtra(MediaStore.EXTRA_OUTPUT, uri)
|
||||||
if (resolveActivity(packageManager) != null) {
|
if (resolveActivity(packageManager) != null) {
|
||||||
startActivityForResult(this, INTENT_CHOOSE_PHOTO)
|
startActivityForResult(this, INTENT_CHOOSE_PHOTO)
|
||||||
@ -470,13 +488,15 @@ class ContactActivity : SimpleActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getCachePhotoFile(): File {
|
private fun getCachePhotoUri(): Uri {
|
||||||
val imagesFolder = File(cacheDir, "my_cache")
|
val imagesFolder = File(cacheDir, "my_cache")
|
||||||
if (!imagesFolder.exists()) {
|
if (!imagesFolder.exists()) {
|
||||||
imagesFolder.mkdirs()
|
imagesFolder.mkdirs()
|
||||||
}
|
}
|
||||||
|
|
||||||
return File(imagesFolder, "Photo_${System.currentTimeMillis()}.jpg")
|
val file = File(imagesFolder, "Photo_${System.currentTimeMillis()}.jpg")
|
||||||
|
val createNewFile = file.createNewFile()
|
||||||
|
return FileProvider.getUriForFile(this, "${BuildConfig.APPLICATION_ID}.provider", file)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getEmailTextId(type: Int) = when (type) {
|
private fun getEmailTextId(type: Int) = when (type) {
|
||||||
|
Reference in New Issue
Block a user