Instantly share code, notes, and snippets.

@illuzor /WallpaperFragment.kt Secret
Last active Sep 29, 2018

Embed
What would you like to do?
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