Skip to content

Instantly share code, notes, and snippets.

@illuzor
Last active September 29, 2018 18:54
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 illuzor/18bd7c791998a90a4ce31100306f98e7 to your computer and use it in GitHub Desktop.
Save illuzor/18bd7c791998a90a4ce31100306f98e7 to your computer and use it in GitHub Desktop.
private fun checkPermissionForImageSave() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
activity!!.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveImageToGallery()
} else {
requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), REQUEST_STORAGE_PERMISSION_CODE)
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
if (requestCode == REQUEST_STORAGE_PERMISSION_CODE && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
saveImageToGallery()
} else {
toast(R.string.unable_to_save_permission)
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
private fun saveImageToGallery() {
val dialog = ProgressDialog()
dialog.isCancelable = false
dialog.title = getString(R.string.save_image)
dialog.show(fragmentManager, "")
runInBackground {
MediaStore.Images.Media.insertImage(context!!.contentResolver, imageFile.path, "wallpaper", "")
runInMainThread {
dialog.dismiss()
toast(R.string.saved)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment