Created
June 13, 2020 10:20
-
-
Save harish81/f5b7257660d82f186ef0d7cac9cc84b3 to your computer and use it in GitHub Desktop.
Image Picker With Cropping Support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//register below activity | |
<activity | |
android:name="com.theartofdev.edmodo.cropper.CropImageActivity" | |
android:theme="@style/Base.Theme.AppCompat" /> | |
<activity android:name=".ImagePickerActivity"></activity> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//build.gradle (app) file | |
//... | |
implementation 'com.aminography:choosephotohelper:1.0.6' | |
implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.+' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//start image picker | |
imgProfile.setOnClickListener { | |
startActivityForResult(Intent(this, ImagePickerActivity::class.java), | |
ImagePickerActivity.KEY_REQUEST_CODE) | |
} | |
//get result in activityResult | |
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | |
super.onActivityResult(requestCode, resultCode, data) | |
if(requestCode == ImagePickerActivity.KEY_REQUEST_CODE){ | |
if(resultCode== Activity.RESULT_OK){ | |
val photoUri = Uri.parse(data?.getStringExtra(ImagePickerActivity.KEY_FILE_URI)) | |
Glide.with(this).load(photoUri).into(binding.imgProfile) | |
byteFileData = ImagePickerActivity.getBytes(this, photoUri) | |
mimeTypeFile = data?.getStringExtra(ImagePickerActivity.KEY_MIME) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity | |
import android.content.Context | |
import android.content.Intent | |
import android.net.Uri | |
import android.os.Bundle | |
import android.webkit.MimeTypeMap | |
import androidx.appcompat.app.AppCompatActivity | |
import com.aminography.choosephotohelper.ChoosePhotoHelper | |
import com.aminography.choosephotohelper.callback.ChoosePhotoCallback | |
import com.theartofdev.edmodo.cropper.CropImage | |
import java.io.ByteArrayOutputStream | |
import java.io.File | |
/** | |
* Image Picker Activity With cropping support | |
*/ | |
class ImagePickerActivity : AppCompatActivity() { | |
lateinit var choosePhotoHelper: ChoosePhotoHelper | |
var resultUri: Uri? = null | |
companion object{ | |
public val KEY_FILE_URI = "file_uri" | |
public val KEY_MIME = "mime-type" | |
public val KEY_REQUEST_CODE = 1007 | |
fun getBytes(context: Context, uri: Uri): ByteArray? { | |
val inputStream = context.contentResolver.openInputStream(uri) | |
if(inputStream!=null) { | |
val byteBuffer = ByteArrayOutputStream() | |
val bufferSize = 1024 | |
val buffer = ByteArray(bufferSize) | |
var len = 0 | |
while (inputStream.read(buffer).also({ len = it }) != -1) { | |
byteBuffer.write(buffer, 0, len) | |
} | |
return byteBuffer.toByteArray() | |
} | |
return null | |
} | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
init() | |
//start chooser | |
choosePhotoHelper.showChooser() | |
} | |
private fun init() { | |
choosePhotoHelper = ChoosePhotoHelper.with(this) | |
.asFilePath() | |
.build(ChoosePhotoCallback<String> { photo -> showCropper(photo) }) | |
} | |
private fun showCropper(photo: String) { | |
resultUri = Uri.fromFile(File(photo)) | |
CropImage.activity(resultUri) | |
.start(this) | |
} | |
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | |
super.onActivityResult(requestCode, resultCode, data) | |
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) { | |
val result = CropImage.getActivityResult(data) | |
if (resultCode == Activity.RESULT_OK) { | |
resultUri = result.uri | |
setImage() | |
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) { | |
val error = result.error | |
} | |
} else { | |
choosePhotoHelper.onActivityResult(requestCode, resultCode, data) | |
} | |
} | |
private fun setImage() { | |
val mimeType: String | |
if(resultUri!=null){ | |
try { | |
mimeType = MimeTypeMap.getFileExtensionFromUrl(resultUri.toString()) | |
//set data | |
val dataResult = Intent() | |
dataResult.putExtra(KEY_MIME, mimeType) | |
dataResult.putExtra(KEY_FILE_URI, resultUri.toString()) | |
setResult(Activity.RESULT_OK, dataResult) | |
} catch (e: Exception) { | |
e.printStackTrace() | |
} | |
} | |
finish() | |
} | |
override fun onRequestPermissionsResult( | |
requestCode: Int, | |
permissions: Array<out String>, | |
grantResults: IntArray | |
) { | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults) | |
choosePhotoHelper.showChooser() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment