Skip to content

Instantly share code, notes, and snippets.

@the-dagger
Last active July 15, 2018 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the-dagger/1882ffca68d6bacbe9b55afbd0deab47 to your computer and use it in GitHub Desktop.
Save the-dagger/1882ffca68d6bacbe9b55afbd0deab47 to your computer and use it in GitHub Desktop.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//Check and request permission to write external storage
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(this, Array<String>(1) { Manifest.permission.WRITE_EXTERNAL_STORAGE }, 12345)
//If there is no permission, disable the onClickListener on the FAB
fab_take_photo.setOnClickListener(null)
} else {
fab_take_photo.setOnClickListener(this)
}
}
override fun onClick(v: View?) {
//onClick attribute for the FloatingActionButton
startIntentForPicker()
}
private fun startIntentForPicker() {
//Start Image Picker from the gallery
EasyImage.openGallery(this, 0)
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == 12345) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
//The library requires write access to the External Storage,
//so ensure that the permission is granted
fab_take_photo.setOnClickListener(this)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
EasyImage.handleActivityResult(requestCode, resultCode, data, this, object : DefaultCallback() {
override fun onImagePicked(imageFile: File?, source: EasyImage.ImageSource?, type: Int) {
// The image was fetched successfully
val bitmap = BitmapFactory.decodeFile(imageFile?.path)
//Make API call with the bitmap
getLandscapeFromCloud(bitmap)
}
override fun onImagePickerError(e: Exception?, source: EasyImage.ImageSource?, type: Int) {
//Some error handling since no image was picked
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment