-
-
Save illuzor/18bd7c791998a90a4ce31100306f98e7 to your computer and use it in GitHub Desktop.
This file contains 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
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