Android Request Permission (READ_EXTERNAL_STORAGE)

September 24, 2019

Edit AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    ...
</manifest>

Activity

class TestPermissionFragment : Fragment() {

    companion object {
        fun newInstance() = TestPermissionFragment()

        private const val REQUEST_STORAGE_PERMISSION = 1
    }


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.test_permission, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        testButton.setOnClickListener {
            if (hasStoragePermission()) {
                findAlbumsInPhotoGallery(context!!.contentResolver)
            }
            else {
                requestStoragePermission()
            }
        }

        startActivity(GalleryActivity.newInstance(context!!))
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        // super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        when(requestCode) {
            REQUEST_STORAGE_PERMISSION -> {
                if (grantResults.isNotEmpty() && grantResults[0] == PERMISSION_GRANTED) {
                    findAlbumsInPhotoGallery(context!!.contentResolver)
                }
                else {
                    val showRational =
                        ActivityCompat.shouldShowRequestPermissionRationale(
                            activity!!,
                            Manifest.permission.READ_EXTERNAL_STORAGE
                        )

                    if (showRational) {
                        Timber.d("Storage permission denied")
                    }
                    else {
                        Intent(ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:${activity!!.packageName}")).apply {
                            addCategory(Intent.CATEGORY_DEFAULT)
                            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        }.also { intent ->
                            startActivity(intent)
                        }
                    }
                }
            }
        }
    }

    private fun hasStoragePermission() = ContextCompat.checkSelfPermission(
        context!!,
        Manifest.permission.READ_EXTERNAL_STORAGE
    ) == PERMISSION_GRANTED

    private fun requestStoragePermission() {
        if (!hasStoragePermission()) {
            val permissions = arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE)
            ActivityCompat.requestPermissions(activity!!, permissions, REQUEST_STORAGE_PERMISSION)
        }
    }

    private fun findAlbumsInPhotoGallery(contentResolver: ContentResolver) {
        // run code which require READ_EXTERNAL_STORAGE permission

    }

}
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.